fsync()方法強制將檔描述符fd寫入到磁片檔。如果已經從一個Python檔對象f 開始,那麼先執行 fflush()然後執行 os.fsync(f.fileno()) ,以確保 f 相關的所有內部緩衝區寫入磁片。
語法
以下是 fsync() 方法的語法:
os.fsync(fd)
參數
-
fd -- 這是需要緩衝同步的檔描述符
返回值
此方法不返回任何值。
示例
下麵的示例演示 fsync() 方法的使用。
#!/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"
b=line.encode()
os.write(fd, b)
# 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)
line = os.read(fd, 100)
b=line.decode()
print ("Read String is : ", b)
# Close opened file
os.close( fd )
print ("Closed the file successfully!!")
當我們運行上面的程式,它會產生以下結果:
Read String is : this is test Closed the file successfully!!
上一篇:
Python3檔方法
下一篇:
Python3 os檔目錄的方法
