Python3 os.ftruncate() Method
Python3 OS File/Directory Methods
Overview
os.ftruncate() truncates the file corresponding to the file descriptor fd so that it cannot exceed the file size.
Available on Unix.
Syntax
The **ftruncate()** method syntax is as follows:
os.ftruncate(fd, length)¶
Parameters
-
fd -- The file descriptor.
-
length -- The size to truncate the file to.
Return Value
This method has no return value.
Example
The following example demonstrates the use of the ftruncate() method:
#!/usr/bin/python3
import os, sys
# Open file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
# Write string
os.write(fd, "This is test - This is test")
# Using ftruncate() method
os.ftruncate(fd, 10)
# Read content
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print ("读取的字符串是 : ", str)
# Close file
os.close( fd)
print ("关闭文件成功!!")
The output of the above program is:
读取的字符串是 : This is te
关闭文件成功!!