Python os.truncate() Method

Musfirah Waseem Jan 30, 2023 Aug 26, 2022
  1. Syntax of Python os.truncate() Method
  2. Example Codes: Use of the os.truncate() Method
  3. Example Codes: Respective Length Exceeds the File’s Size in the os.truncate() Method
  4. Example Codes: Use the os.truncate() Method to Delete a File
Python os.truncate() Method

Python os.truncate() method truncates a file indicated by a specific path. A specific length supplied as a parameter causes the file to be truncated.

Syntax of Python os.truncate() Method

os.truncate(path address, length)

Parameters

path address A file system path or a symlink address object. The object can either be an str or bytes.
length An integer value that specifies the number of bytes that the file should be reduced to.

Return

The Python os.truncate() method does not return any value.

Example Codes: Use of the os.truncate() Method

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.truncate(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.truncate() method is equivalent to os.ftruncate() method.

Example Codes: Respective Length Exceeds the File’s Size in the os.truncate() Method

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.truncate(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

The File.txt content did not change up to its original size. The file was filled with random invalid characters to increase the file to the provided length.

Example Codes: Use the os.truncate() Method to Delete a File

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.truncate(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.truncate() 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