Python read function
last modified November 29, 2026 Python 3.15
In this article we show how to use the read method to take part of a file or the whole of it. We look at the size argument, the empty string that marks the end of the file, the difference between characters and bytes, and reading in chunks.
Opening and closing files is explained in Python open function and Python close function. Reading a whole file for its own sake is covered in Python read file, and reading line by line in Python readline function.
The text file
Most examples read a small file of city names, saved in UTF-8 with one name per line. The file has six lines and ends with a newline character.
Paris Prague Vienna Kraków Budapest Warsaw
The accented ó in Kraków takes two bytes in UTF-8 but counts as a single character, which matters for every method that counts one or the other.
Reading a number of characters
read takes an optional size and returns at most that many characters in text mode. Each call starts where the previous one stopped, and a size of zero returns an empty string without moving the position.
with open('cities.txt', encoding='utf-8') as f:
print(repr(f.read(5)))
print(repr(f.read(8)))
print(repr(f.read(0)))
Five characters give the first name without its newline. The second call begins at that newline and takes it together with the next name, eight characters in all. The third call asks for nothing and returns an empty string, while the position stays where it was.
$ python read_chars.py 'Paris' '\nPrague\n' ''
Reading to the end
Without a size, read returns everything from the current position to the end of the file.
with open('cities.txt', encoding='utf-8') as f:
print(repr(f.read(6)))
rest = f.read()
print(len(rest))
print(rest.splitlines()[0])
After read(6) the position is just past Paris\n, so the rest has 37 characters and begins with Prague. Reading the rest in one call is convenient for small files; for a large file, read in chunks or iterate over the lines, as Python read file and Python readlines function show.
$ python read_end.py 'Paris\n' 37 Prague
The end of the file
At the end of the file read returns an empty string, not None and not an error. Further calls keep returning the same empty string.
with open('cities.txt', encoding='utf-8') as f:
print(len(f.read()))
print(repr(f.read(5)))
print(repr(f.read()))
The file has 43 characters. After the first call the position is at the end, so both the sized and the unsized call return ''. A loop can therefore stop as soon as read returns an empty string.
$ python read_eof.py 43 '' ''
Negative sizes
A negative size also means read to the end. The two modes differ in what they accept: a text handle takes any negative number, while a binary handle takes only -1 and raises ValueError for a smaller value.
with open('cities.txt', encoding='utf-8') as f:
whole = f.read(-1)
with open('cities.txt', encoding='utf-8') as f:
other = f.read(-2)
print(whole == other, len(whole))
with open('raw.bin', 'wb') as f:
f.write(b'ABCDE')
with open('raw.bin', 'rb') as f:
try:
f.read(-2)
except ValueError as e:
print(type(e).__name__, e)
Both -1 and -2 return the whole text file, so the comparison is True and the length is 43 characters. In binary mode -2 raises ValueError, and the message names the one accepted negative value. Passing None also reads to the end in both modes.
$ python read_all.py True 43 ValueError read length must be non-negative or -1
Characters, not bytes
In text mode the size counts characters after decoding, so it does not match the number of bytes stored in the file when the text is not pure ASCII.
with open('cities.txt', encoding='utf-8') as f:
text = f.read()
print(len(text))
print(len(text.encode('utf-8')))
start = text.index('Krak')
word = text[start:start + 6]
print(word, len(word), len(word.encode('utf-8')))
The file holds 43 characters but 44 bytes, because ó takes two bytes. The six characters of Kraków occupy seven bytes. Slicing the decoded string is therefore the right way to take a fixed number of characters; a byte count belongs to binary mode.
$ python read_characters.py 43 44 Kraków 6 7
Binary mode
In binary mode read returns bytes and the size counts bytes exactly. No decoding and no newline translation take place.
with open('raw.bin', 'wb') as f:
f.write(b'AB\x00\xffCD')
with open('raw.bin', 'rb') as f:
print(repr(f.read(2)))
print(repr(f.read(2)))
print(repr(f.read(10)))
print(repr(f.read(10)))
The file holds six bytes: two letters, a null byte, 0xff, and two more letters. The first two calls take two bytes each, the third takes the remaining two, and the fourth returns b''. A text handle would have tried to decode 0xff as UTF-8 and failed.
$ python read_binary.py b'AB' b'\x00\xff' b'CD' b''
Reading in chunks
Reading a large file in fixed-size pieces keeps memory use constant. The loop stops when read returns an empty string.
with open('cities.txt', encoding='utf-8') as f:
while True:
chunk = f.read(20)
if not chunk:
break
print(len(chunk), repr(chunk))
Three chunks of 20, 20, and 3 characters cover the 43-character file. The last chunk is shorter because the file ends; nothing is padded. Joining the chunks gives back the original text, which is how a copy loop or a checksum works.
$ python read_chunks.py 20 'Paris\nPrague\nVienna\n' 20 'Kraków\nBudapest\nWars' 3 'aw\n'
Source
TextIOBase.read – Python documentation
Methods of File Objects – Python Tutorial
In this article we have read a file in pieces and as a whole, counted characters in text mode and bytes in binary mode, and stopped at the empty string that marks the end. See also Python read file, Python readline function, Python readlines function, Python open function, and Python close function.
Author
List all Python tutorials.