File path

The location of a file or folder, as text or a pathlib Path object.

A file path tells the program where a file lives. Paths can be absolute (from the drive root) or relative (from the current folder). Because separators and rules differ across operating systems, the modern approach is pathlib’s Path, which builds and inspects paths safely with the / operator.

Example
from pathlib import Path

p = Path("data") / "reports" / "june.csv"
print(p.name)        # june.csv
print(p.suffix)      # .csv
print(p.parent)      # data/reports
Output
june.csv
.csv
data/reports

Where this shows up in real Python

File paths appear whenever you read or write files: opening a config, saving a report, or walking a folder of images.

Path tools

  • Path('a') / 'b.txt' — join path parts safely
  • .name / .suffix / .parent — filename, extension, folder
  • .exists() — check if it is there
  • .absolute() — get the full path
  • Path.home(), Path.cwd() — home and current folders

Official documentation: Python Library Reference: pathlib

Related lessons

Related terms