Python | os.ftruncate () method

Python Methods and Functions

os.ftruncate() in Python is used to truncate a file corresponding to a specified file descriptor to a specified length.

This method is equivalent to os.truncate (fd, length) .

Syntax: os.ftruncate (fd, length)

Parameters:
fd : The file descriptor representing the file to be truncated.
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 following text as the content of a 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: Using the os.ftruncate () method

# Python program to explain the os.ftruncate () method

  
# import of the os module

import os

 

 
# Open the file and get
# the file descriptor associated
# using the os method .open ()

fd = os . open ( "Python_intro.txt" , os.O_RDWR) 

 

  
# Print the original file size (in bytes)

print ( "File size (in bytes): " , os.stat (fd) .st_size)

  

 
# Length (in bytes) up to
# file will be truncated

length = 72

  
# Truncate file
# up to maximum specified length
# using the os.ftruncate () method
os.ftruncate (fd, length)

 
# Print file content

size = os.stat (fd) .st_size

print (os.read (fd, size). decode ( "utf-8" ))

 
# Print file size (in bytes)

print ( "File size (in bytes):" , os.stat (fd) .st_size)

Exit:

 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 following the same text as the new content of a 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

# Python program to explain the os.ftruncate () method

 
# import of the os module

import os

 
# Open the file and get
# the file descriptor associated
# using the os.open () method

fd = os. open   ( "Python_intro.txt" , os.O_RDWR) 

 

 
# Print the original file size (in bytes)

print ( " File size (in bytes): " , os.stat (fd) .st_size)

  
# Length (in bytes) to which
# the file will be truncated

length = 100

 
# Truncate file
# up to the maximum length
# using the os.ftruncate () method
os. ftruncate (fd, length)

 
# Print file content

size = os.stat (fd) .st_size

print (os.read (fd, size) .decode ( "utf-8" ))

 
# Print file size (in bytes)

print ( "File size (in bytes):" , os.stat (fd) .st_size)

Exit :

 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 truncation file size from 72 to 100 bytes:

The content of the file has not changed to its original size, but to increase the size file to the specified size, it was filled with some invalid characters.

Code # 3: Deleting the contents of a file using the os.ftruncate () method

# Python program to explain the os.ftruncate () method

 
# import of the os module

import os

 

 
# Open the file and get
# file descriptor associated
# using the os.open () method

fd = os. open ( " Python_intro.txt " , os.O_RDWR) 

 

 
# Print the original file size (in bytes)

print ( "File size (in bytes):" , os.stat (fd) .st_size)

 
# specify length as 0
# delete file content

length = 0

 
# Truncate file # up to length 0
# using the os.ftruncate () method
os.ftruncate (fd, length)

 
# Print file content

size = os.stat (fd) .st_size

print (os.read (fd, size) .decode ( "utf-8" ))

 
# Print file size (in bytes)

print ( "File size (in bytes):" , os.stat (fd) .st_size)

Exit:

 File size ( in bytes): 100 Content of file Python_intro.txt: File siz e (in bytes): 0 




Get Solution for free from DataCamp guru