Python os.ftruncate() Method

Python os.ftruncate() Method

  1. Syntax of the os.ftruncate() Method
  2. Example Codes: Working With the os.ftruncate() Method in Python
  3. Example Codes: Using the os.ftruncate() Method With Length Exceeding the File Size in Python
  4. Example Codes: Delete a File With the os.ftruncate() Method in Python

Python os.ftruncate() method is an efficient way of truncating a file corresponding to a respective file descriptor. The file is truncated to a specified length entered as a parameter.

Syntax of the os.ftruncate() Method

os.ftruncate(file descriptor, length)

Parameters

file descriptor The file descriptor that must be truncated.
length It is an integer value representing a length in bytes to which the file is to be reduced.

Return

In the execution process, this method does not return any value.

Example Codes: Working With the os.ftruncate() Method in Python

import os

with open('File.txt', 'w') as file:
    file.write('Hello, World! Programming is so fun!')

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

print("The file size, in bytes, is:", os.stat(fd).st_size)

length = 15

os.ftruncate(fd, length)

size = os.stat(fd).st_size

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

print("The file size, in bytes, after truncating:", os.stat(fd).st_size)

Output:

The file size, in bytes, is: 36
Hello, World! P
The file size, in bytes, after truncating: 15

The os.ftruncate() method is equivalent to another Python method os.truncate().

Example Codes: Using the os.ftruncate() Method With Length Exceeding the File Size in Python

import os

with open('File.txt', 'w') as file:
    file.write('Hello, World! Programming is so fun!')

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

print("The file size, in bytes, is:", os.stat(fd).st_size)

length = 100

os.ftruncate(fd, length)

size = os.stat(fd).st_size

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

print("The file size, in bytes, after truncating:", os.stat(fd).st_size)

Output:

The file size, in bytes, is: 36
Hello, World! Programming is so fun!
The file size, in bytes, after truncating: 100

In the above code, File.txt content did not change to its original size. But to increase the file size to the entered length, it got filled with random invalid characters.

Example Codes: Delete a File With the os.ftruncate() Method in Python

import os

with open('File.txt', 'w') as file:
    file.write('Hello, World! Programming is so fun!')

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

print("The file size, in bytes, is:", os.stat(fd).st_size)

length = 0

os.ftruncate(fd, length)

size = os.stat(fd).st_size

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

print("The file size, in bytes, after truncating:", os.stat(fd).st_size)

Output:

The file size, in bytes, is: 36

The file size, in bytes, after truncating: 0

The os.ftruncate() method resizes the file to a specified number of bytes. However, the current position is used if the length is not given as a parameter.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python OS

  • Python os.set_handle_inheritable() Method
  • Python os.set_inheritable() Method
  • Python os.stat_result Class
  • Python os.renames() Method
  • Python os.get_handle_inheritable Method
  • Python os.get_inheritable Method