Python os.fsync() 方法

python os.fsync() 方法

python os 文件/目錄方法python os 文件/目錄方法

os.fsync() 方法強(qiáng)制將文件描述符為fd的文件寫入硬盤。在unix, 將調(diào)用fsync()函數(shù);在windows, 調(diào)用 _commit()函數(shù)。

如果你準(zhǔn)備操作一個(gè)python文件對(duì)象f, 首先f.flush(),然后os.fsync(f.fileno()), 確保與f相關(guān)的所有內(nèi)存都寫入了硬盤.在unix,windows中有效。

unix、windows上可用。

 

語(yǔ)法

fsync()方法語(yǔ)法格式如下:

os.fsync(fd)

 

參數(shù)

  • fd -- 文件的描述符。

 

返回值

該方法沒有返回值。

 

實(shí)例

以下實(shí)例演示了 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)
# 讀取內(nèi)容
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print "讀取的字符串為 : ", str
# 關(guān)閉文件
os.close( fd)
print "關(guān)閉文件成功!!"

執(zhí)行以上程序輸出結(jié)果為:

讀取的字符串為 :  this is test
關(guān)閉文件成功!!

python os 文件/目錄方法python os 文件/目錄方法

下一節(jié):python os.ftruncate() 方法

python 教程

相關(guān)文章
亚洲国产精品第一区二区,久久免费视频77,99V久久综合狠狠综合久久,国产免费久久九九免费视频