Python-os-fsync

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

Python os.fsync()メソッド

説明

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

構文

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

os.fsync(fd)

パラメーター

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

戻り値

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

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

#!/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")

# 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)
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 test
Closed the file successfully!!