Python Os.ftruncate() Method



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


Following is the syntax for ftruncate() method −

os.ftruncate(fd, length)


  • fd − This is the file descriptor, which needs to be truncated.
  • length − This is the length of the file where file needs to be truncated.

Return Value

This method does not return any value.


The following example shows the usage of ftruncate() method.


import os, sys

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

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

# Now you can use ftruncate() method.
os.ftruncate(fd, 10)

# Now read this file from the beginning.
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print "Read String is : ", str

# Close opened file
os.close( fd )

print "Closed the file successfully!!"

When we run the above program, it produces the following result:

Read String is :  This is te
Closed the file successfully!!

Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.