The truncate()
method in Python is a built-in method used to resize the file to a specified size. It removes all the data after the specified position and sets the file size to the specified size. If the specified size is less than the current size, then the data after the specified position will be lost. If the specified size is greater than the current size, then the file will be padded with null bytes.
Syntax
file.truncate(size=None)
The size
parameter is an optional parameter that specifies the new size of the file. If the size parameter is not specified, then the file will be truncated to the current position of the file pointer.
Return Value
The truncate()
method returns nothing. It modifies the file in place.
Examples
Example 1: Truncate a file to a specified size
In this example, we will use the truncate()
method to resize a file to a specified size:
file = open("example.txt", "r+") file.write("Hello, World!") file.truncate(5) file.close()
In this example, we opened a file “example.txt” in read and write mode, wrote the string “Hello, World!” to the file using the write()
method, and used the truncate()
method to resize the file to 5 bytes. The data after the fifth byte is removed, and the file size is set to 5 bytes.
Output:
Hello
Example 2: Truncate a file to the current position of the file pointer
In this example, we will use the truncate()
method to resize a file to the current position of the file pointer:
file = open("example.txt", "r+") file.write("Hello, World!") file.seek(5) file.truncate() file.close()
In this example, we opened a file “example.txt” in read and write mode, wrote the string “Hello, World!” to the file using the write()
method, moved the file pointer to the fifth byte using the seek()
method, and used the truncate()
method to resize the file to the current position of the file pointer. The data after the fifth byte is removed, and the file size is set to 5 bytes.
Output:
Hello
Example 3: Truncate a file to a smaller size
In this example, we will use the truncate()
method to resize a file to a smaller size:
file = open("example.txt", "r+") file.write("Hello, World!") file.truncate(5) file.close()
In this example, we opened a file “example.txt” in read and write mode, wrote the string “Hello, World!” to the file using the write()
method, and used the truncate()
method to resize the file to 5 bytes. Since the specified size is less than the current size, the data after the fifth byte is removed, and the file size is set to 5 bytes.
Output:
Hello
Use Cases
The truncate()
method can be used in various scenarios where we need to resize a file to a specified size or truncate the file to the current position of the file pointer. It is commonly used in file handling operations where we need to remove unwanted data from a file or resize a file to a specific size.
Conclusion
The truncate()
method in Python is a useful method for resizing a file to a specified size or truncating a file to the current position of the file pointer. It is easy to use and can be used in various file handling scenarios. It is important to keep in mind that when using the truncate()
method, the data after the specified position will be lost if the specified size is less than the current size of the file.