Easy Tutorial
❮ Python String Find Python File Read ❯

Python3 os.ftruncate() Method

Python3 OS File/Directory Methods


Overview

The os.ftruncate() method truncates the file corresponding to file descriptor fd, so that it is at most length bytes in size.

Available on Unix.

Syntax

The syntax for the ftruncate() method is as follows:

os.ftruncate(fd, length)¶

Parameters

-

fd -- The file descriptor of the file.

-

length -- The size to which the file is to be truncated.

Return Value

This method does not return any value.

Example

The following example demonstrates the use of the ftruncate() method:

#!/usr/bin/python3

import os, sys

# Open a file
fd = os.open("foo.txt", os.O_RDWR|os.O_CREAT)

# Write a string
os.write(fd, "This is test - This is test")

# Use the ftruncate() method
os.ftruncate(fd, 10)

# Read the content
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print("The read string is : ", str)

# Close the file
os.close(fd)

print("Closed the file successfully!!")

Executing the above program outputs:

The read string is :  This is te
Closed the file successfully!!

Python3 OS File/Directory Methods

❮ Python String Find Python File Read ❯