Home >>Python File Methods >Python File truncate() Method
Python File truncate() Method in python file is used to truncate (resizes) the file size to the given number of bytes. The position of the current file is not changed.
Syntax:file.truncate(size)
Parameter | Description |
---|---|
size | It is Optional. To truncate the size of the file. |
z = open("file1.txt", "a")
z.truncate(25)
z.close()
z = open("file2.txt", "r")
print(z.read())
y= open("file2.txt", "a")
y.truncate(25)
y.close()
y = open("file2.txt", "r")
print(y.read())