Python3 os.ftruncate() 方法
概述
os.ftruncate() 裁剪檔描述符fd對應的檔, 它最大不能超過檔大小。
Unix上可用。
語法
ftruncate()方法語法格式如下:
os.ftruncate(fd, length)¶
參數
fd -- 檔的描述符。
length -- 要裁剪檔大小。
返回值
該方法沒有返回值。
實例
以下實例演示了 ftruncate() 方法的使用:
#!/usr/bin/python3
import os, sys
# 打開檔
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
# 寫入字串
os.write(fd, "This is test - This is test")
# 使用 ftruncate() 方法
os.ftruncate(fd, 10)
# 讀取內容
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print ("讀取的字串是 : ", str)
# 關閉檔
os.close( fd)
print ("關閉檔成功!!")
執行以上程式輸出結果為:
讀取的字串是 : This is te 關閉檔成功!!

Python3 OS 檔/目錄方法