前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >linux fsync实例,Python os.fsync()方法「建议收藏」

linux fsync实例,Python os.fsync()方法「建议收藏」

作者头像
全栈程序员站长
发布2022-09-03 17:41:22
发布2022-09-03 17:41:22
6840
举报

大家好,又见面了,我是你们的朋友全栈君。

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

语法

以下是fsync()方法的语法 –

os.fsync(fd)

参数

fd − 这是缓冲区同步的文件描述符(必需的)。

返回值

此方法没有返回值。

示例

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

#!/usr/bin/python3

import os, sys

# Open a file

fd = os.open( “foo.txt”, os.O_RDWR|os.O_CREAT )

# Write one string

line = “this is test”

b = line.encode()

os.write(fd, b)

# 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)

line = os.read(fd, 100)

b = line.decode()

print (“Read String is : “, b)

# Close opened file

os.close( fd )

print (“Closed the file successfully!!”)

执行上面代码后,将得到以下结果 –

Read String is : this is test

Closed the file successfully!!

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/138386.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月3,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档