Python File truncate() Method
Example
Open the file in "a" mode for appending and then truncate the file to 20 bytes:
f = open("demofile2.txt", "a") f.truncate(20) f.close() #open and read the file after the truncate: f = open("demofile2.txt", "r") print(f.read())
Definition and Usage
The truncate() method adjusts the file size to the specified number of bytes.
If the size is not specified, the current position will be used.
Syntax
file.truncate(size)
Parameter Value
Parameter | Description |
---|---|
size | Optional. The size of the truncated file (in bytes). Default value: None, indicating the current file stream position. |