Python的os.fdatasync()
方法強制將與fd
相關聯的檔寫入到磁片。這不會強制更新元數據。如果要刷新緩衝區,則可以使用此方法。
語法
以下是fdatasync()
方法的語法 -
os.fdatasync(fd, uid, gid)
參數
- 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!!
上一篇:
Python os模組方法
下一篇:
Python異常處理