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

当无涯教程运行上面的程序时,它产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/python/os-fsync.html

来源:LearnFk无涯教程网

Read String is :  This is test
Closed the file successfully!!

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

技术教程推荐

朱赟的技术管理课 -〔朱赟〕

程序员进阶攻略 -〔胡峰〕

编辑训练营 -〔总编室〕

TypeScript开发实战 -〔梁宵〕

Web安全攻防实战 -〔王昊天〕

深度学习推荐系统实战 -〔王喆〕

Django快速开发实战 -〔吕召刚〕

大数据经典论文解读 -〔徐文浩〕

深入拆解消息队列47讲 -〔许文强〕

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