Python3 os.ftruncate()方法

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檔目錄的方法