Python os.fsync() 方法
python os.fsync() 方法
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)閉文件成功!!
相關(guān)文章
- Python break 語(yǔ)句
- Python 字典 Dictionary
- Python 函數(shù)
- Python File 方法
- Python 正則表達(dá)式
- Python XML 解析
- Python 數(shù)據(jù)結(jié)構(gòu)
- Python 環(huán)境
- Python 矩陣
- Python Deque
- Python 二叉樹
- Python 搜索樹
- Python 堆
- Python 算法分析
- Python 樹遍歷算法
- Python 排序算法
- Python3 注釋
- Python3 文件讀寫
- Python3 OS 文件/目錄方法
- Python3 日期和時(shí)間