Python file truncate – Python File truncate() Method with Examples

Files in Python:

Truncate file python: One of the most important subjects for programmers and automation testers is Python file handling (also known as File I/O). It is necessary to work with files in order to write to or read data from them.

Also, if you didn’t know, I/O operations are the most expensive processes where a programme can go wrong. As a result, you must use extreme caution while implementing file processing for reporting or any other reason. Optimizing a single file action can help in the creation of a high-performing application or a reliable automated software testing solution.

Consider the following scenario: you’re planning to construct a large Python project with a large number of workflows. Then it’s unavoidable that you don’t make a log file. You’ll also be handling the log file’s read and write activities. Debugging huge applications with log files is a terrific way to go. It’s usually better to consider a scalable design from the start, as you won’t be sorry later if you didn’t.

File truncate() Method in Python:

Python file truncate: The truncate() method resizes the size of a file to the specified number of bytes.

If no size is given, the current position is taken.

Syntax:

file.truncate(size)

Parameters

size: This is Optional. The file’s size (in bytes) after truncation. The default value is None, which represents the current file stream position.

File truncate() Method with Examples in Python

Approach:

  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in append mode. In this case, we’re simply appending the contents into the file.
  • Pass some random number as an argument to the truncate() function to truncate the file lines to the given number.
  • Close the given file using the close function.
  • Open the file in read mode. In this case, we’re simply reading the contents of the file.
  • Read the contents of the file using the read() function.
  • The Exit of Program.

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Open the file in append mode. In this case, we're simply appending the contents into the file.
gvn_file = open(givenFilename, 'a') 
# Pass some random number as an argument to the truncate() function to truncate the filelines 
# to the given number
gvn_file.truncate(15)
# Close the given file using the close function
gvn_file.close()

# Open the file in read mode. In this case, we're simply reading the contents of the file.
gvn_file = open(givenFilename, 'r') 
# Read the contents of the file using the read() function
print(gvn_file.read())

Output:

Hello this is b

Original File Content:

Hello this is btechgeeks
Good morning btechgeeks
welcome to python-coding platform

File Content after truncating to the given number of bytes:

Hello this is b

Google Colab Images:

Files and Code: