Python-os-ftruncate
提供:Dev Guides
Python os.ftruncate()メソッド
説明
Pythonのメソッド* ftruncate()*は、ファイル記述子fdに対応するファイルを切り捨て、サイズが最大で長さバイトになります。
構文
以下は* ftruncate()*メソッドの構文です-
os.ftruncate(fd, length)
パラメーター
- fd -これはファイル記述子であり、切り捨てる必要があります。
- 長さ-これは、ファイルを切り捨てる必要があるファイルの長さです。
戻り値
このメソッドは値を返しません。
例
次の例は、ftruncate()メソッドの使用法を示しています。
#!/usr/bin/python
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!!