.truncate() file method allows the user to resize the file to a given number of bytes when the file is accessed through the append mode.
.truncate() to change the size of the gullivers_travels.txt file from 603,908 bytes to 100:
f = open("gullivers_travels.txt", "a")f.truncate(100)f.close()f = open("gullivers_travels.txt", "r")print(f.read())
Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.
- Anonymous contributor
Learn Python on Codecademy
- Anonymous contributorAnonymous contributor