ftruncate()方法截断对应的文件描述符fd文件,所以,它为最长度(length)大小字节。
语法
下面是 ftruncate() 方法的语法:
os.ftruncate(fd, length)
参数
-
fd -- 这是需要被截断的文件描述符
-
length -- 这是需要被截断的文件的长度
返回值
此方法不返回任何值。可用于UNIX类系统。
示例
下面的示例显示 ftruncate() 方法的使用。
#!/usr/bin/python3
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!!")
当我们运行上面的程序,它会产生以下结果:
Read String is : This is te Closed the file successfully!!
上一篇:
Python3文件方法
下一篇:
Python3 os文件目录的方法
