Python3-os-fsync

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

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

説明

メソッド* fsync()*は、ファイル記述子fdを持つファイルをディスクに強制的に書き込みます。 Pythonファイルオブジェクトfから開始する場合は、最初にf.flush()を実行し、次にos.fsync(f.fileno())を実行して、fに関連付けられているすべての内部バッファーがディスクに書き込まれるようにします。

構文

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

os.fsync(fd)

パラメーター

*fd* -これは、バッファ同期が必要なファイル記述子です。

戻り値

このメソッドは値を返しません。

次の例は、fsync()メソッドの使用方法を示しています。

#!/usr/bin/python3
import os, sys

# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Write one string
line = "this is test"
b = line.encode()
os.write(fd, b)

# Now you can use fsync() method.
# Infact here you would not be able to see its effect.
os.fsync(fd)

# Now read this file from the beginning
os.lseek(fd, 0, 0)
line = os.read(fd, 100)
b = line.decode()
print ("Read String is : ", b)

# Close opened file
os.close( fd )

print ("Closed the file successfully!!")

結果

上記のプログラムを実行すると、次の結果が生成されます-

Read String is :  this is test
Closed the file successfully!!