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!!

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

技术教程推荐

React实战进阶45讲 -〔王沛〕

Go语言核心36讲 -〔郝林〕

大规模数据处理实战 -〔蔡元楠〕

接口测试入门课 -〔陈磊〕

NLP实战高手课 -〔王然〕

MySQL 必知必会 -〔朱晓峰〕

程序员的测试课 -〔郑晔〕

编程高手必学的内存知识 -〔海纳〕

超级访谈:对话张雪峰 -〔张雪峰〕

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