Python os.fsync() 方法
概述
os.fsync() 方法強制將檔描述符為fd的檔寫入硬碟。在Unix, 將調用fsync()函數;在Windows, 調用 _commit()函數。
如果你準備操作一個Python檔對象f, 首先f.flush(),然後os.fsync(f.fileno()), 確保與f相關的所有記憶體都寫入了硬碟.在unix,Windows中有效。
Unix、Windows上可用。
語法
fsync()方法語法格式如下:
os.fsync(fd)
參數
fd -- 檔的描述符。
返回值
該方法沒有返回值。
實例
以下實例演示了 fsync() 方法的使用:
#!/usr/bin/python # -*- coding: UTF-8 -*- import os, sys # 打開檔 fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT ) # 寫入字串 os.write(fd, "This is test") # 使用 fsync() 方法. os.fsync(fd) # 讀取內容 os.lseek(fd, 0, 0) str = os.read(fd, 100) print "讀取的字串為 : ", str # 關閉檔 os.close( fd) print "關閉檔成功!!"
執行以上程式輸出結果為:
讀取的字串為 : This is test 關閉檔成功!!