Python provides several inbuilt modules and functions to work with files. All of these modules and functions are used to perform various operations, such as renaming files, appending or removing files, and resizing files. To resize or truncate the file based on the specified size, the inbuilt “file.truncate()” method is used in Python.
This article presents an extensive guide on Python’s “file.truncate()” method utilizing multiple examples and via the following content:
What is the File “file.truncate()” Method in Python?
The “file.truncate” method is utilized in Python to truncate or resize the specified file based on the particular bytes numbers.
Syntax
Parameter
In the above syntax:
-
- The “truncate()” method takes the “size” parameter, which indicates the file size after the truncation.
- The present position will be utilized if the size parameter is not defined.
Return Value
The “truncate()” method does not retrieve any value.
Now, let’s perform some examples using this method on the following text file named “example.txt”:
The size of the file is shown below:
Example 1: Truncating a File to Specified Size Using “file.truncate()” Method
The following code utilize the “file.truncate()” method to truncate the file size:
file_obj.truncate(25)
file_obj.close()
In the above code, the “open()” function opens the file named “example.txt” in append mode “a”. The “file.truncate()” method takes the bytes number “25” as an argument and reduces the size of the file. At last, the file is closed using the “file.close()” method.
Output
The file content after the truncation is shown in the above snippet.
The file size after the truncation is shown in this snippet.
Example 2: Truncating and Reading File Using “truncate()” and “read()” Methods
We can also read the file content in the Python console after truncating the file to the specified bytes. Here is an example code:
file_obj.truncate(25)
file_obj.close()
file_obj = open("example.txt", "r")
print(file_obj.read())
In this code, the “file.truncate()” method takes the specified bytes size “25” as an argument and truncates the file. The “read()” method opens the file in the “r” mode and reads the file data after the truncation.
Output
The truncated file has been read successfully.
Example 3: Truncating a File By “file.truncate()” Method Using “with” Statement
The “with” statement can also be used along with the “file.truncate()” method to resize the file in Python. The benefit of using the “with” statement is that we don’t need to close the file after truncation, as it automatically closes the file. Here is an example code:
fp.truncate(25)
In the above code, the “with” is used with the “open()” function to open the file in “a” append mode and truncate it using the “truncate()” method.
Output
The file has been truncated successfully.
Conclusion
In Python, the “truncate()” method is used to truncate or reduce the file size by taking the specified bytes numbers as an argument. The “truncate()” method is used along with the “read()” method to truncate and read the content of the file in Python. This blog presented a thorough overview of the Python “truncate()” method utilizing multiple examples.