Python3-os-fdatasync

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

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

説明

メソッド* fdatasync()は、ファイル記述子 *fd を使用してファイルをディスクに強制的に書き込みます。 これはメタデータの更新を強制しません。 バッファをフラッシュする場合は、このメソッドを使用できます。

構文

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

os.fdatasync(fd)

パラメーター

*fd* -これは、データが書き込まれるファイル記述子です。

戻り値

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

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

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

# string needs to be converted byte object
b = str.encode(line)
os.write(fd, b)

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

# Now read this file from the beginning.
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
line = os.read(fd2, 100)
str = line.decode()
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!!