Python的os.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!!
上一篇:
Python os模組方法
下一篇:
Python異常處理