Python的os.fdatasync()
方法強制將與fd
相關聯的檔案寫入到磁碟。這不會強制更新後設資料。如果要重新整理緩衝區,則可以使用此方法。
語法
以下是fdatasync()
方法的語法 -
os.fdatasync(fd, uid, gid)
引數
返回值
範例
以下範例顯示了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!!