linux fsync实例,Python os.fsync()方法

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

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值