Home >>Python File Methods >Python File truncate() Method

Python File truncate() Method

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 Values

Parameter Description
size It is Optional. To truncate the size of the file.
Here is an example of Python File truncate() Method:

z = open("file1.txt", "a")
z.truncate(25)
z.close()
z = open("file2.txt", "r")
print(z.read())

Output:
Hello! Welcome to de
Example 2:

y= open("file2.txt", "a")
y.truncate(25)
y.close()
y = open("file2.txt", "r")
print(y.read())

Output:
Hello! Welcome to de

No Sidebar ads