To read files in Python you need one function, which is open(), and one habit, which is to keep that call inside a with block. That pairing gives you an explicit file mode, a file object that closes itself when the block ends, and a handle that you can walk through one line at a time.
This is the first lesson in the Python File Handling and Exceptions module, and the next lesson in this module is about "Writing Files: Text, Newlines and CSV".
Here is the whole pattern in one breath. with open(path, "r", encoding="utf-8") as handle: binds an open file object to the name handle, and every read that you want goes in the indented block underneath it. When that block finishes, whether it finishes normally or because something went wrong, Python closes the file for you. Everything else on this page is detail that sits on top of that one shape.
What do the letters in a file mode actually mean?
The second argument to open() is a short mode string. The first character decides whether you can read, write, or do both. An optional + character upgrades a mode so that it does both at once, without truncating the file.
| Mode | Meaning | If the file does not exist | Existing content |
|---|---|---|---|
r | read only | raises FileNotFoundError | left untouched |
w | write only | created empty | truncated, which means erased |
a | append only | created empty | kept, and the new data goes to the end |
x | exclusive create | created empty | raises FileExistsError |
r+ | read and write | raises FileNotFoundError | kept, and the cursor starts at position 0 |
The mode that bites beginners is w. It does not mean "write and keep what was there", it means "write, and if something was there, throw it away first". When you want to add to a file rather than replace it, that is the a mode. The x mode is the safe one for anything that a machine generates, because it fails loudly instead of quietly overwriting a file that you care about.
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
log = Path(tmp) / 'app.log'
log.write_text('first run\n', encoding='utf-8')
with log.open('a', encoding='utf-8') as handle:
handle.write('second run\n')
print(log.read_text(encoding='utf-8'))
print("mode 'w' would have thrown the first line away")
Why does every open() call belong inside a with block?
An open file is a live handle that the operating system keeps track of, and the operating system has a hard limit on how many of them one process may hold. Forget to close one and you eventually hit an OSError: [Errno 24] Too many open files. On Windows the handle also stays locked, so the user cannot delete or rename the file while your script is still running.
Python's garbage collector will eventually close a file object that you forgot about, but "eventually" is not a plan. The with statement closes the file at a moment that you choose, which is the end of the block. It does so even when the block raises an error, so the cleanup happens on the error path as well. The with statement is not special for files at all. open() simply returns an object that implements the context manager protocol, which is why handle.closed becomes True the moment the block ends.
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
target = Path(tmp) / 'notes.txt'
target.write_text('alpha\nbeta\n', encoding='utf-8')
with target.open(encoding='utf-8') as handle:
print('inside the block, closed is:', handle.closed)
print('first line:', handle.readline().strip())
print('after the block, closed is:', handle.closed)
target.unlink()
print('file deleted, so the handle really was released')
There is a subtle benefit here as well. A with block keeps the lifetime of the handle tied to the scope that uses it, so there is no way to accidentally use a closed file later in the same function and get a confusing ValueError: I/O operation on closed file instead of a clear FileNotFoundError.
How do you read a file line by line instead of loading all of it?
Python gives you three ways to pull text back out of a file, and they differ enormously in how much memory they use. handle.read() returns everything that is left as one single string. handle.readlines() returns a list of strings, one per line. Iterating over the handle directly, with for line in handle:, hands you one line at a time and never builds the rest of the file in memory.
For a file of unknown size, iterate over the handle. For a small configuration file that you intend to search as a whole, read() is convenient and often faster, because it asks the operating system for one big block instead of many small ones. A useful middle ground is readline(), which returns the next single line and lets you walk a file forward one step at a time.
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
target = Path(tmp) / 'notes.txt'
target.write_text('alpha\nbeta\ngamma\n', encoding='utf-8')
with target.open(encoding='utf-8') as handle:
print('read() ->', repr(handle.read()))
with target.open(encoding='utf-8') as handle:
print('readlines() ->', [line.strip() for line in handle.readlines()])
with target.open(encoding='utf-8') as handle:
number = 0
for line in handle:
number += 1
print('line', number, ':', line.rstrip())
One detail trips people up, and that is line endings. read() returns the line separators exactly as they appear on disk, so a file that was created on Windows and read on Linux shows \r\n inside the strings. readline() and readlines() behave in the same way. If you want clean text, strip the trailing newline yourself with line.rstrip("\n") or line.strip().
What happens when the file is not there?
Opening a missing file for reading raises FileNotFoundError, and that error is a subclass of OSError. The inheritance is not a formality, because it lets you write one handler for "something went wrong at the filesystem level" and a narrower handler for "the path is wrong" when you want to treat those two cases differently.
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
missing = Path(tmp) / 'does-not-exist.txt'
try:
missing.read_text(encoding='utf-8')
except FileNotFoundError:
print('no such file:', missing.name)
except OSError as exc:
print('other filesystem problem:', exc)
else:
print('this only runs when nothing went wrong')
Notice the order of the except clauses. Python checks them from the top down and runs the first one that matches, so the specific FileNotFoundError handler has to come before the broad OSError handler. Put them the other way round and the specific handler becomes dead code that never runs.
How do you control the encoding and move the cursor around?
If you leave out encoding, Python uses whatever the machine's locale says, and that is different on a developer's laptop, on a CI runner, and inside a Docker container. Pass encoding="utf-8" explicitly and your script reads the same bytes in the same way everywhere. Python 3.15 changes the default to UTF-8, but on Python 3.12 and 3.13 the explicit argument is still the habit worth forming.
Handles that are open in text mode also let you move the cursor. handle.tell() reports the current position, and handle.seek(offset) jumps to one, with the whence argument choosing the reference point. The reference point is the start of the file, the current position, or the end of the file. In text mode tell() returns an opaque cookie rather than a plain byte count, which is why it is rarely a number that you will compare against directly.
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
target = Path(tmp) / 'latin.txt'
target.write_bytes('café\n'.encode('latin-1'))
with target.open(encoding='latin-1') as handle:
print('decoded as latin-1:', handle.readline().strip())
print('cursor after one line:', handle.tell())
with target.open(encoding='utf-8') as handle:
try:
handle.read()
except UnicodeDecodeError:
print('utf-8 cannot decode those bytes')
The UnicodeDecodeError in that last example is worth understanding properly. It means that the bytes in the file are not valid in the encoding that you asked for. Real world files are sometimes Latin-1 or UTF-16, and the fix is to open the file with the correct encoding rather than to catch the error and carry on.
Key takeaways
- Always pair
open()withwith. The file closes at the end of the block, including on the error path. - Pass the mode explicitly.
"r"reads,"w"writes and truncates,"a"appends, and"x"refuses to overwrite. - Pass
encoding="utf-8"explicitly, so that behaviour does not drift with the machine's locale. - Iterate over the handle for large files, and use
read()for small ones that you intend to treat as a whole. FileNotFoundErroris anOSError, so order yourexceptclauses from specific to general.
Try it yourself: build a line-numbered log reader
Write a script that asks for a filename, opens it for reading, and prints every line with its line number in front of it, skipping the blank lines. Then extend it so that a missing file prints a friendly message instead of a traceback, and so that the path is opened with encoding="utf-8".
Finish with a scale test. Create a file of 100,000 numbered lines using a loop, and then time two versions of your reader, one that calls read() and one that iterates over the handle. The iterating version holds one line in memory at a time, and that is the habit you want for any file that you did not write yourself.