os.fdatasync() 方法



描述

fdatasync() 方法強制將檔案描述符 fd 對應的檔案寫入磁碟。這不會強制更新元資料。如果您想重新整理緩衝區,可以使用此方法。

語法

以下是 fdatasync() 方法的語法:

os.fdatasync(fd)

引數

  • fd - 這是要寫入資料的檔案描述符。

返回值

此方法不返回值。

示例

以下示例演示了 fdatasync() 方法的使用:

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(fd, 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!!
python_os_file_directory_methods.htm
廣告