Python read file
last modified November 18, 2026 Python 3.15
In this article we show how to read files in Python. We read text files
line by line or at once, move around a file with seek and
tell, handle errors, and read binary data.
Encodings, decoding errors, and newline handling of text files are covered in a separate article, Python read text.
The open function
To read a file, we first open it with the built-in open
function. It returns a file object whose type depends on the mode.
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
The file is a path given as a string or a path-like object
such as pathlib.Path. The mode says how the file
is opened. The buffering sets the buffering policy. The
encoding names the encoding used to decode a text file, the
errors says how decoding errors are handled, and the
newline controls the translation of line endings.
The file modes are:
| Mode | Meaning |
|---|---|
| 'r' | open for reading (default) |
| 'w' | open for writing, truncating the file first |
| 'x' | create a new file and open it for writing; fail if it exists |
| 'a' | open for writing, appending to the end of the file if it exists |
| 'b' | binary mode |
| 't' | text mode (default) |
| '+' | open for updating (reading and writing) |
The modes combine: 'r' and 'rt' read text,
'rb' reads bytes, and 'r+' reads and writes an
existing file. In text mode, reading returns str; in binary
mode, it returns bytes.
The text file
The text examples use the following file with some of Honoré de Balzac's works.
Lost Illusions Beatrix Honorine The firm of Nucingen Old Goriot Colonel Chabert Cousin Bette Gobseck César Birotteau The Chouans
The file has ten lines and is saved in UTF-8. The é in
César takes two bytes.
Reading the whole file
The read method without an argument reads from the current
position to the end of the file and returns a single string.
with open('works.txt', 'r', encoding='utf-8') as f:
contents = f.read()
print(contents, end='')
print(f.closed)
The example reads the whole file and prints its contents.
with open('works.txt', 'r', encoding='utf-8') as f:
We open the file for reading in text mode. The with statement
makes the file object a context manager: the file is closed automatically
when the block ends, even when an exception is raised. Since Python 3.15,
open uses UTF-8 when no encoding is given (PEP 686); older
versions use the locale encoding. We pass the encoding explicitly so that the
program behaves the same everywhere.
print(f.closed)
After the with block, the file is closed, but the
contents variable still holds the data.
$ python read_all.py Lost Illusions Beatrix Honorine The firm of Nucingen Old Goriot Colonel Chabert Cousin Bette Gobseck César Birotteau The Chouans True
Reading characters
The size argument of read limits the number of
characters read. In text mode, the size counts characters; in binary mode,
it counts bytes.
with open('works.txt', encoding='utf-8') as f:
print(repr(f.read(4)))
print(repr(f.read(20)))
print(repr(f.read(10)))
rest = f.read()
print(len(rest))
print(repr(f.read()))
We read 4, 20, and 10 characters, then the rest of the file. We print the
strings with repr to make the newlines visible.
rest = f.read() print(len(rest)) print(repr(f.read()))
Each read continues where the previous one stopped. At the end of the file,
read returns an empty string.
$ python read_characters.py 'Lost' ' Illusions\nBeatrix\nH' 'onorine\nTh' 95 ''
The readline method
The readline method reads up to and including the next newline
and returns the line. With a size argument, it reads at most
that many characters. At the end of the file, it returns an empty string.
with open('works.txt', encoding='utf-8') as f:
print(repr(f.readline()))
print(repr(f.readline()))
print(repr(f.readline(5)))
print(repr(f.readline()))
We read two full lines, then five characters of the third line, and the rest of that line.
$ python read_line.py 'Lost Illusions\n' 'Beatrix\n' 'Honor' 'ine\n'
Reading lines in a while loop
Because readline returns an empty string only at the end of
the file, it can drive a while loop. An empty line in the
middle of the file is returned as '\n', which is true.
with open('works.txt', encoding='utf-8') as f:
while line := f.readline():
if line.startswith('C'):
print(line, end='')
print(repr(f.readline()))
We print the works whose title begins with C.
while line := f.readline():
The assignment expression (walrus operator) reads a line and tests it in one step.
$ python read_line_loop.py Colonel Chabert Cousin Bette César Birotteau ''
The readlines method
The readlines method reads all remaining lines into a list.
Each line keeps its newline character.
with open('works.txt', encoding='utf-8') as f:
lines = f.readlines()
print(len(lines))
print(lines[:3])
print(repr(lines[-1]))
We read the lines into a list and print its length, the first three lines, and the last line.
Like read, readlines loads the whole file into
memory. It is convenient when we need random access to the lines.
$ python read_lines.py 10 ['Lost Illusions\n', 'Beatrix\n', 'Honorine\n'] 'The Chouans\n'
Iterating over a file
A file object is an iterator over its lines. Iterating over it in a
for loop is the idiomatic and most memory-efficient way to
process a text file.
with open('works.txt', encoding='utf-8') as f:
for nr, line in enumerate(f, start=1):
print(f'{nr:2}: {line}', end='')
We print the lines with line numbers. The enumerate function
counts from 1, and the :2 format spec right-aligns the numbers.
$ python iterate_file.py 1: Lost Illusions 2: Beatrix 3: Honorine 4: The firm of Nucingen 5: Old Goriot 6: Colonel Chabert 7: Cousin Bette 8: Gobseck 9: César Birotteau 10: The Chouans
Searching a file
A common task is to find the lines that contain some text, like the
grep tool does.
def grep(fname, word):
with open(fname, encoding='utf-8') as f:
return [(nr, line.rstrip()) for nr, line in enumerate(f, 1)
if word.lower() in line.lower()]
for nr, line in grep('works.txt', 'the'):
print(nr, line)
The grep function returns the numbers and contents of the lines
that contain the given word, ignoring case.
return [(nr, line.rstrip()) for nr, line in enumerate(f, 1)
if word.lower() in line.lower()]
The list comprehension iterates over the file lazily; only the matching lines are kept.
$ python search_file.py 4 The firm of Nucingen 10 The Chouans
The last lines of a file
A deque with a maximum length keeps only the last items added
to it. Filling it from a file gives us the last lines, like the
tail tool.
from collections import deque
with open('works.txt', encoding='utf-8') as f:
tail = deque(f, maxlen=3)
for line in tail:
print(line, end='')
The file is read line by line; the deque discards older lines as new ones arrive, so only three lines are ever stored.
$ python last_lines.py Gobseck César Birotteau The Chouans
The tell and seek methods
The tell method returns the current position in the file and
the seek method moves to a new position.
seek(offset, whence=os.SEEK_SET)
The offset is interpreted relative to whence:
os.SEEK_SET (0) is the start of the file,
os.SEEK_CUR (1) the current position, and
os.SEEK_END (2) the end of the file.
In text mode, the value returned by tell is an opaque number,
not a character count, and the only valid arguments to seek are
values returned by tell and 0.
with open('works.txt', encoding='utf-8') as f:
print(f.tell())
f.readline()
pos = f.tell()
print(pos)
print(f.readline(), end='')
f.seek(pos)
print(f.readline(), end='')
f.seek(0)
print(f.readline(), end='')
We remember the position after the first line, read the second line, go back, and read the second line again. Then we rewind to the start.
f.seek(0)
Seeking to 0 is the usual way to read a file again without reopening it.
$ python telling.py 0 15 Beatrix Beatrix Lost Illusions
Seeking in binary mode
In binary mode, positions are byte offsets, and all three
whence values can be used with any offset.
import io
import os
with open('works.txt', 'rb') as f:
f.seek(-12, os.SEEK_END)
print(f.read())
f.seek(5)
print(f.read(9))
f.seek(3, os.SEEK_CUR)
print(f.tell(), f.read(8))
with open('works.txt', encoding='utf-8') as f:
try:
f.seek(-12, os.SEEK_END)
except io.UnsupportedOperation as e:
print('error:', e)
We open the file in binary mode and read from several positions.
f.seek(-12, os.SEEK_END) print(f.read())
We move 12 bytes before the end of the file and read the last line, which
is returned as bytes.
f.seek(3, os.SEEK_CUR)
After reading nine bytes from offset 5, we are at offset 14; skipping three bytes takes us to offset 17.
f.seek(-12, os.SEEK_END)
In text mode, a nonzero offset relative to the end is not allowed and
raises io.UnsupportedOperation.
$ python seeking.py b'The Chouans\n' b'Illusions' 17 b'atrix\nHo' error: can't do nonzero end-relative seeks
Handling errors
Opening a file can fail for many reasons. All of them raise subclasses of
OSError, such as FileNotFoundError,
IsADirectoryError, and PermissionError.
for fname in ['works.txt', 'missing.txt', '.']:
try:
with open(fname, encoding='utf-8') as f:
print(fname, len(f.read()))
except FileNotFoundError as e:
print('not found:', e.filename)
except IsADirectoryError as e:
print('directory:', e.filename)
except OSError as e:
print('error:', e)
We try to read an existing file, a missing file, and a directory.
except OSError as e:
print('error:', e)
The more specific exceptions are listed first; OSError catches
the rest. The filename attribute of the exception holds the
file name.
The existing file has 129 characters; it has 130 bytes because of the
two-byte é.
$ python read_errors.py works.txt 129 not found: missing.txt directory: .
Reading without the with statement
Without with, we must close the file ourselves, also when an
error occurs. The try/finally statement guarantees
that.
f = None
try:
f = open('works.txt', encoding='utf-8')
first = f.readline().rstrip()
print(first)
except OSError as e:
print(e)
finally:
if f is not None:
f.close()
print(f.closed)
The finally block runs whether or not an exception was raised.
We check for None because open itself might have
failed, in which case there is nothing to close. The with
statement does all of this for us and should be preferred.
$ python try_finally.py Lost Illusions True
Reading a binary file
In binary mode, no decoding takes place: read returns the raw
bytes. The binary examples use a small PNG image, sid.png, a
48x32 pixel red rectangle.
with open('sid.png', 'rb') as f:
data = f.read()
print(type(data), len(data))
print(data[:8])
print(data[:24].hex(' '))
We read the whole image and print the first bytes.
print(data[:24].hex(' '))
The hex method of bytes returns a hexadecimal
string; the optional separator is placed between the bytes.
Every PNG file starts with the same eight-byte signature, followed by the
IHDR chunk with the image size.
$ python read_binary.py <class 'bytes'> 106 b'\x89PNG\r\n\x1a\n' 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 30 00 00 00 20
Parsing a binary header
Binary formats are usually read field by field. The struct
module converts bytes to numbers according to a format string.
import struct
with open('sid.png', 'rb') as f:
signature = f.read(8)
if signature != b'\x89PNG\r\n\x1a\n':
raise SystemExit('not a PNG file')
length, chunk_type = struct.unpack('>I4s', f.read(8))
width, height, depth, colour = struct.unpack('>IIBB', f.read(10))
print(chunk_type.decode(), length)
print(f'{width}x{height}, bit depth {depth}, colour type {colour}')
We check the PNG signature and read the image width, height, bit depth, and
colour type from the IHDR chunk.
length, chunk_type = struct.unpack('>I4s', f.read(8))
The > character selects big-endian byte order, which PNG
uses. I is a four-byte unsigned integer and 4s a
four-byte string.
width, height, depth, colour = struct.unpack('>IIBB', f.read(10))
The width and height are four-byte integers, the bit depth and colour type are single bytes. Colour type 2 means RGB.
$ python png_header.py IHDR 13 48x32, bit depth 8, colour type 2
Reading in chunks
Large binary files should be processed in fixed-size chunks so that memory use stays constant.
import hashlib
total = 0
chunks = 0
with open('works.txt', 'rb') as f:
while chunk := f.read(32):
total += len(chunk)
chunks += 1
print(total, chunks)
with open('sid.png', 'rb') as f:
digest = hashlib.file_digest(f, 'sha256')
print(digest.hexdigest())
We read the text file in 32-byte chunks and compute the SHA-256 digest of the image.
while chunk := f.read(32):
The loop ends when read returns an empty bytes
object at the end of the file. The 130-byte file is read in five chunks.
digest = hashlib.file_digest(f, 'sha256')
The hashlib.file_digest function, added in Python 3.11, does
the chunked reading for us.
$ python chunks.py 130 5 95b94fac0a3e562204d1b9b800229f2634b8558cc04e56e328118f890d291eea
Reading files with pathlib
The pathlib.Path class has convenience methods that open, read,
and close a file in one call: read_text and
read_bytes. It can also check whether the file exists and get
its size.
from pathlib import Path
path = Path('works.txt')
print(path.exists(), path.is_file(), path.stat().st_size)
text = path.read_text(encoding='utf-8')
print(text.count('\n'), 'lines')
data = Path('sid.png').read_bytes()
print(len(data), data[1:4])
We check the text file, count its lines, and read the image bytes.
print(len(data), data[1:4])
Slicing bytes returns bytes; the bytes 1 to 3 of
the signature spell PNG.
$ python read_path.py True True 130 10 lines 106 b'PNG'
Source
open – Python built-in functions
Reading and writing files – Python tutorial
In this article we have read text and binary files in Python. See also Python read text, Python pathlib, Python list directory, and Python bytes.
Author
List all Python tutorials.