Python3-os-ftruncate

提供:Dev Guides
移動先:案内検索

Python 3-os.ftruncate()メソッド

説明

メソッド* ftruncate()*は、ファイル記述子fdに対応するファイルを切り捨て、サイズが最大でlengthバイトになるようにします。

構文

以下は* ftruncate()*メソッドの構文です-

os.ftruncate(fd, length)

パラメーター

  • fd -これはファイル記述子であり、切り捨てる必要があります。
  • 長さ-これは、ファイルを切り捨てる必要があるファイルの長さです。

戻り値

このメソッドは値を返しません。 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!!