.truncate()
net137228473814 total contributions
Published May 10, 2021Updated Aug 11, 2023
Contribute to Docs
The .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.
Syntax
file.truncate(n)
Example
Use .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())
Codebyte Example
In this example .truncate()
is used on my_one_liner.txt
reducing the size by two bytes or two characters:
All contributors
- net137228473814 total contributions
- Anonymous contributorAnonymous contributor3 total contributions
- Anonymous contributorAnonymous contributor15 total contributions
- christian.dinh2476 total contributions
- Anonymous contributorAnonymous contributor3071 total contributions
- Anonymous contributorAnonymous contributor186 total contributions
- net1372284738
- Anonymous contributor
- Anonymous contributor
- christian.dinh
- Anonymous contributor
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.