OS File 中的 os.fsync(fd)函数

首页 / Python2入门教程 / OS File 中的 os.fsync(fd)函数

Python方法fsync()强制将具有文件描述符fd的文件写入磁盘。如果您从Python文件对象f开始,首先执行f.flush(),然后执行os.fsync(f.fileno(),以确保与f关联的所有内部缓冲区都写入磁盘。

os.fsync(fd) - 语法

os.fsync(fd)
  • fd    -  这是缓冲区同步所需的文件描述符。

os.fsync(fd) - 示例

以下示例显示了fsync()方法的用法。

#!/usr/bin/python

import os, sys

# Open a file
fd=os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Write one string
os.write(fd, "This is test")

# 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)
str=os.read(fd, 100)
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!!

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

邱岳的产品手记 -〔邱岳〕

研发效率破局之道 -〔葛俊〕

说透中台 -〔王健〕

OAuth 2.0实战课 -〔王新栋〕

Redis核心技术与实战 -〔蒋德钧〕

性能优化高手课 -〔尉刚强〕

说透区块链 -〔自游〕

零基础实战机器学习 -〔黄佳〕

快手 · 音视频技术入门课 -〔刘歧〕

好记忆不如烂笔头。留下您的足迹吧 :)