Python | os.truncate() method
Last Updated :
11 Oct, 2021
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.
os.truncate()
method in Python is used to truncate the file indicated by the specified path to at most specified length.
Syntax: os.truncate(path, length)
Parameters:
path: A path-like object representing a file system path. This will indicate the file to be truncated.
A path-like object is either a string or bytes object representing a path.
length: An integer value denoting length (in bytes) to which the file is to be truncated.
Return Type: This method does not return any value.
Consider the below text as the content of the file named Python_intro.txt.
Python is a widely used general-purpose, high level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code. Python is a programming language that lets you work quickly and integrate systems more efficiently.
Code #1: Use of os.truncate() method
import os
path = "/home / ihritik / Desktop / Python_intro.txt"
print ( "File size (in bytes):" , os.path.getsize(path))
length = 72
os.truncate(path, length)
print ( "Content of file Python_intro.txt:" )
with open (path, 'r' ) as f:
print (f.read())
print ( "File size (in bytes):" , os.path.getsize(path))
|
Output:
File size (in bytes): 409
Content of file Python_intro.txt:
Python is a widely used general-purpose, high level programming language
File size (in bytes): 72
Consider the below text as the new content of the file named Python_intro.txt.
Python is a widely used general-purpose, high level programming language
Code #2: If the specified length exceeds the file size
import os
path = "/home / ihritik / Desktop / Python_intro.txt"
print ( "File size (in bytes):" , os.path.getsize(path))
length = 72
os.truncate(path, length)
print ( "Content of file Python_intro.txt:" )
with open (path, 'r' ) as f:
print (f.read())
print ( "File size (in bytes):" , os.path.getsize(path))
|
Output:
File size (in bytes): 72
Content of file Python_intro.txt:
Python is a widely used general-purpose, high level programming language
File size (in bytes): 100
Actual file content after truncating file sized 72 bytes to 100 bytes:
The file content up to its original size did not changed but to increase the file size to the specified size it got filled with some invalid characters.
Code #3: Deleting a file content using os.truncate() method
import os
path = "/home / ihritik / Desktop / Python_intro.txt"
print ( "File size (in bytes):" , os.path.getsize(path))
length = 0
os.truncate(path, length)
print ( "Content of file Python_intro.txt:" )
with open (path, 'r' ) as f:
print (f.read())
print ( "File size (in bytes):" , os.path.getsize(path))
|
Output:
File size (in bytes): 100
Content of file Python_intro.txt:
File size (in bytes): 0
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...