Python | os.ftruncate() method

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.ftruncate() method in Python is used to truncate the file corresponding to the specified file descriptor to the specified length.

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



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 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.ftruncate() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.ftruncate() method 
    
# importing os module 
import os
  
  
# Open the file and get 
# the file descriptor associated 
# with it using os.open() method
fd = os.open("Python_intro.txt", os.O_RDWR) 
  
  
# Print the original size of the file (in bytes)
print("File size (in bytes):", os.stat(fd).st_size)
  
  
# Length (in Bytes) to which 
# the file will be trunctated
length = 72
  
# Truncate the file 
# to at most given length
# using os.ftruncate() method
os.ftruncate(fd, length)
  
# Print the content of file
size = os.stat(fd).st_size
print(os.read(fd, size).decode("utf-8"))
  
# Print the size of file (in bytes)
print("File size (in bytes):", os.stat(fd).st_size)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.ftruncate() method 
    
# importing os module 
import os
  
# Open the file and get 
# the file descriptor associated 
# with it using os.open() method
fd = os.open("Python_intro.txt", os.O_RDWR) 
  
  
# Print the original size of the file (in bytes)
print("File size (in bytes):", os.stat(fd).st_size)
  
# Length (in Bytes) to which 
# the file will be trunctated
length = 100
  
# Truncate the file 
# to at most given length
# using os.ftruncate() method
os.ftruncate(fd, length)
  
# Print the content of file
size = os.stat(fd).st_size
print(os.read(fd, size).decode("utf-8"))
  
# Print the size of file (in bytes)
print("File size (in bytes):", os.stat(fd).st_size)

chevron_right


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:
Python_intro.txt
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.ftruncate() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.ftruncate() method 
    
# importing os module 
import os
  
  
# Open the file and get 
# the file descriptor associated 
# with it using os.open() method
fd = os.open("Python_intro.txt", os.O_RDWR) 
  
  
# Print the original size of file (in bytes)
print("File size (in bytes):", os.stat(fd).st_size)
  
# specify the length as 0
# to delete the file content
length = 0
  
# Truncate the file 
# to length 0
# using os.ftruncate() method
os.ftruncate(fd, length)
  
# Print the content of file
size = os.stat(fd).st_size
print(os.read(fd, size).decode("utf-8"))
  
# Print the size of file (in bytes)
print("File size (in bytes):", os.stat(fd).st_size)

chevron_right


Output:

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


My Personal Notes arrow_drop_up

self motivated and passionate programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.