Skip to main content

How to Open a File with Path.open in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

When a file's location is a pathlib.Path, you can open it straight from the path with Path.open, the same way the built-in open() works. This guide writes a line to a file and reads it back, on Python 3.15.2.

Syntax​

Syntax
from pathlib import Path

with Path("<file>").open("<mode>", encoding="utf-8") as <file>:
...
PartWhat it means
Path("<file>")The file's path
"<mode>""r" to read (the default, so you can leave it out), "w" to write over the file, "a" to add to its end
encoding="utf-8"How the text is stored as bytes
with ... as <file>:Closes the file when the block ends, even if something fails inside it

The pathlib reference says Path.open will "Open the file pointed to by the path, like the built-in open() function does".

The Example​

A script writes one line of a to-do list, Pump the tyres, to notes.txt, then opens the file again and prints what's in it.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

notes.py
from pathlib import Path

notes = Path("notes.txt")

with notes.open("w", encoding="utf-8") as file:
file.write("Pump the tyres\n")

with notes.open(encoding="utf-8") as file:
print(file.read(), end="")

The notes.py script in a macOS-style code window: notes = Path("notes.txt"), one with block that opens it in "w" mode and writes Pump the tyres, and a second with block that opens it for reading and prints it.

end="" stops print from adding a second line break after the one already in the file.

Step 2: Run It​

Terminal
uv run python notes.py
Output
Pump the tyres

The terminal after uv run python notes.py: Pump the tyres.

The first block created notes.txt and wrote the line; the second read it back. Each with block closed the file at its end. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Opening a file that doesn't exist for readingFileNotFoundError: [Errno 2] No such file or directory: 'missing.txt'Check the name and folder, or create the file first with mode "w"
Using "w" when you meant to add a line"w" empties the file before writingUse "a" to append

Troubleshooting​

SymptomLikely causeFix
FileNotFoundError for a file you can seeThe script runs in a different folder from the fileRun it from the file's folder, or build the full path with Path(__file__).parent

FAQ​

Path.open or open()?​

They do the same thing. Path.open reads more naturally when you already have a Path, and the built-in open() accepts a Path too.

Why pass encoding="utf-8" when Python 3.15 uses UTF-8 anyway?​

Because not every Python does. The built-in open() reference says: "In text mode, if encoding is not specified, UTF-8 is used by default; if Python UTF-8 Mode is disabled, locale.getencoding() is called to get the current locale encoding." Naming the encoding gives the same result everywhere.

Conclusion​

notes.open("w", encoding="utf-8") wrote the file and notes.open(encoding="utf-8") read it back, each inside a with block that closed it. Name the encoding, and expect a FileNotFoundError when the file isn't where the path says.

Additional Resources​