Pythonファイルtruncate()メソッド
例
追加するために「a」を付けてファイルを開き、ファイルを20バイトに切り捨てます。
f = open("demofile2.txt", "a")
f.truncate(20)
f.close()
#open
and read the file after the truncate:
f = open("demofile2.txt", "r")
print(f.read())
定義と使用法
このtruncate()
メソッドは、ファイルを指定されたバイト数にサイズ変更します。
サイズが指定されていない場合は、現在の位置が使用されます。
構文
file.truncate(size)
パラメータ値
Parameter | Description |
---|---|
size | Optional. The size of the file (in bytes) after the truncate. Default None, which means the current file stream position. |