Working with File Paths in Python

A file path tells Python where a file or folder is located.

When you read, write, rename, or delete files, Python needs a path. This page shows what file paths are, how relative and absolute paths work, and the safest beginner-friendly way to build paths in Python.

Quick example

from pathlib import Path

file_path = Path("data") / "notes.txt"
print(file_path)
print(file_path.exists())

Use pathlib.Path to build paths safely instead of typing slashes by hand.

Possible output:

data/notes.txt
False

What this page covers

  • What a file path is
  • The difference between absolute and relative paths
  • How the current working directory affects file paths
  • How to build paths safely with pathlib
  • How to check whether a path exists before opening a file

What a file path is

A file path is the location of a file or folder.

Examples:

  • A file path: report.txt
  • A file path inside a folder: data/report.txt
  • A folder path: documents

Python uses paths in tasks like:

  • Opening a file
  • Saving a file
  • Checking whether a file exists
  • Creating folders
  • Deleting files

For example, when you use open(), the path tells Python which file to open.

with open("notes.txt", "r") as file:
    content = file.read()
    print(content)

In this example, "notes.txt" is the file path.

Absolute vs relative paths

There are two common kinds of file paths.

Absolute paths

An absolute path starts from the full system location.

Examples:

  • Windows: C:\Users\Sam\Documents\notes.txt
  • macOS/Linux: /home/sam/documents/notes.txt

An absolute path does not depend on where your script is running.

Relative paths

A relative path starts from the current working directory.

Examples:

  • notes.txt
  • data/notes.txt
  • ../notes.txt

Relative paths are shorter and often easier to move between computers. But they only work if Python starts in the folder you expect.

Quick comparison

  • Absolute paths
    • Clearer for debugging
    • Do not depend on the working directory
    • Less portable between computers
  • Relative paths
    • Shorter and cleaner
    • Better for projects you move or share
    • Depend on the current working directory

The current working directory

The current working directory is the folder Python is using as its starting point.

When you use a relative path, Python looks for the file from that folder.

For example:

from pathlib import Path

print(Path.cwd())

This prints the current working directory.

If your code uses this path:

from pathlib import Path

file_path = Path("data") / "notes.txt"
print(file_path.exists())

Python looks for data/notes.txt inside the current working directory.

If the working directory is not what you expected, Python may not find the file. This is one of the most common reasons for FileNotFoundError.

Why pathlib is the best starting point

pathlib is the best place for beginners to start working with paths.

Why it helps:

  • It is part of Python’s standard library
  • It is easier to read than older path code
  • It works across Windows, macOS, and Linux
  • It lets you join path parts with the / operator

Basic example:

from pathlib import Path

path = Path("data") / "notes.txt"
print(path)

This is usually better than typing separators by hand.

Building paths safely

A common beginner mistake is manually combining path parts like this:

folder = "data"
filename = "notes.txt"
path = folder + "/" + filename
print(path)

This may work, but it is easy to make mistakes.

A safer way is to use Path:

from pathlib import Path

path = Path("data") / "notes.txt"
print(path)

You can also build longer paths:

from pathlib import Path

path = Path("projects") / "python" / "files" / "notes.txt"
print(path)

This avoids separator problems across operating systems.

Useful path checks

Before opening a file, it is often helpful to check the path first.

Check whether a path exists

from pathlib import Path

path = Path("data") / "notes.txt"
print(path.exists())

Check whether it is a file

from pathlib import Path

path = Path("data") / "notes.txt"
print(path.is_file())

Check whether it is a directory

from pathlib import Path

path = Path("data")
print(path.is_dir())

These checks can help prevent errors before you try to open a file.

If you want a full example, see how to check if a file exists in Python.

Converting paths to strings

Many Python functions accept Path objects directly.

For example, open() works with a Path object:

from pathlib import Path

file_path = Path("data") / "notes.txt"

with open(file_path, "r") as file:
    print(file.read())

If you ever need a string version, convert it with str():

from pathlib import Path

file_path = Path("data") / "notes.txt"
print(str(file_path))

Most beginners do not need to convert paths unless a specific library requires a string.

Common mistakes

These are some common file path problems:

  • Using the wrong current working directory
  • Typing a relative path that does not match the folder Python is using
  • Building paths by hand with the wrong slash character
  • Mistaking a folder path for a file path
  • Trying to open a file before checking whether it exists

Helpful debugging commands:

from pathlib import Path
print(Path.cwd())
from pathlib import Path
print(Path("data/notes.txt").exists())
from pathlib import Path
p = Path("data/notes.txt")
print(p.resolve())
import os
print(os.getcwd())
import os
print(os.path.exists("data/notes.txt"))

Path.resolve() is especially useful because it shows the full path Python is trying to use.

FAQ

Should beginners use pathlib or os.path?

Use pathlib first. It is easier to read and usually simpler for beginners.

If you want to learn the older style too, see os.path.join() explained.

Why does my relative path work in one place but not another?

Because relative paths depend on the current working directory, not just the script file location.

Can I pass a Path object to open()?

Yes. open() accepts Path objects in modern Python.

What is the difference between a file path and a directory path?

A file path points to a file. A directory path points to a folder.

See also

Once you understand paths, the next step is using them in real tasks like checking for files, opening them, reading them, and writing new ones.