Python truncate function
last modified November 29, 2026 Python 3.15
In this article we show how to use the truncate method to resize a file. We look at shrinking and growing, the current-position form, byte counts in text mode, the writable requirement, and what happens to the file position.
Writing is covered in Python write function, and moving the position in Python seek function. Opening a file for updating is explained in Python open function.
The text file
The examples use the small file of city names, saved in UTF-8 with one name per line. It is opened read-only; the examples that change a file write their own work.txt.
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.
Shrinking a file
truncate(size) cuts the file to that many bytes and returns the new size. Everything after that point is discarded.
with open('work.txt', 'w', encoding='utf-8') as f:
f.write('Paris\nPrague\nVienna\nKraków\nBudapest\nWarsaw\n')
with open('work.txt', 'r+', encoding='utf-8') as f:
print(f.truncate(12))
f.seek(0)
print(repr(f.read()))
Twelve bytes keep Paris\n and Prague, and the read after seek(0) returns exactly that. The accented character is not involved because the cut falls on an ASCII boundary.
$ python truncate_shrink.py 12 'Paris\nPrague'
Truncating at the current position
With no argument, truncate uses the current position. In binary mode that position is exact.
with open('work.txt', 'w', encoding='utf-8') as f:
f.write('Paris\nPrague\nVienna\nKraków\nBudapest\nWarsaw\n')
with open('work.txt', 'rb+') as f:
f.readline()
f.readline()
print(f.tell())
print(f.truncate())
f.seek(0)
print(repr(f.read()))
Two lines are 13 bytes, truncate() returns 13, and the file now ends after Prague. The read confirms that Vienna and the rest are gone.
$ python truncate_position.py 13 13 b'Paris\nPrague\n'
The text-mode surprise
In text mode reading is buffered, and the no-argument form uses the buffer's position, which may already be at the end of the file. Seeking to the logical position first, or passing the size, avoids the problem.
with open('work.txt', 'w', encoding='utf-8') as f:
f.write('Paris\nPrague\nVienna\nKraków\nBudapest\nWarsaw\n')
with open('work.txt', 'r+', encoding='utf-8') as f:
f.readline()
f.readline()
print(f.tell())
print(f.truncate())
with open('work.txt', 'r+', encoding='utf-8') as f:
f.readline()
f.readline()
f.seek(f.tell())
print(f.truncate())
tell reports 13, but truncate() returns 44 and changes nothing, because the read-ahead filled the buffer. In the second block seek(f.tell()) synchronizes the buffer with the logical position, and truncate() then returns 13. This is a good reason to pass the size explicitly.
$ python truncate_text_position.py 13 44 13
Growing a file
A size larger than the file extends it. The new area is filled with null bytes.
with open('work.txt', 'w', encoding='utf-8') as f:
f.write('abc')
with open('work.txt', 'r+', encoding='utf-8') as f:
print(f.truncate(8))
f.seek(0)
print(repr(f.read()))
The file grows from 3 to 8 bytes, and repr shows abc followed by five \x00 characters. Most text tools treat such a file as binary data, so grow only files whose format expects the padding.
$ python truncate_grow.py 8 'abc\x00\x00\x00\x00\x00'
Bytes, not characters
truncate counts bytes even in text mode, so a size can fall inside a multibyte character.
with open('work.txt', 'w', encoding='utf-8') as f:
f.write('Kraków\n')
with open('work.txt', 'r+', encoding='utf-8') as f:
print(f.truncate(5))
with open('work.txt', 'rb') as f:
print(f.read())
Five bytes keep Krak plus the first byte of ó, shown as b'Krak\xc3'. Reading that file as UTF-8 would now fail, because the last byte starts a sequence that is not finished. Choose a size on a character boundary, or work in binary mode.
$ python truncate_bytes.py 5 b'Krak\xc3'
A writable file is required
truncate changes the file, so the handle must be open for writing. A read-only handle raises UnsupportedOperation.
with open('cities.txt', encoding='utf-8') as f:
try:
f.truncate(10)
except OSError as e:
print(type(e).__name__, e)
The error message is just truncate, and the exception is both OSError and ValueError. The fixture is unchanged, because nothing was written.
$ python truncate_readonly.py UnsupportedOperation truncate
The position after truncate
truncate does not move the position. If the position was beyond the new end, the next read returns an empty string.
with open('work.txt', 'w', encoding='utf-8') as f:
f.write('Paris\nPrague\nVienna\n')
with open('work.txt', 'r+', encoding='utf-8') as f:
f.seek(20)
print(f.truncate(5))
print(f.tell())
print(repr(f.read()))
seek(20) is at the end of the 20-byte file, truncate(5) cuts it to five bytes and returns 5, and the position stays at 20. The read returns '', because it starts beyond the end. Seek back to the start before reading the shortened file.
$ python truncate_pointer.py 5 20 ''
Emptying a file
A size of zero removes all content and is the quickest way to reuse a file.
with open('work.txt', 'w', encoding='utf-8') as f:
f.write('x' * 100)
with open('work.txt', 'r+', encoding='utf-8') as f:
print(f.truncate(0))
print(repr(f.read()))
truncate(0) returns 0 and the read returns ''. Opening with 'w' would do the same at open time; truncate is useful when the handle has to stay open, for example in a log that is cleared in place.
$ python truncate_empty.py 0 ''
Source
IOBase.truncate – Python documentation
Methods of File Objects – Python Tutorial
In this article we have cut a file to a size, used the current position, extended a file with null bytes, kept text truncation on character boundaries, and seen why the no-argument form needs care in text mode. See also Python write function and Python seek function.
Author
List all Python tutorials.