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!!
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/138386.html原文链接:https://javaforall.cn
相关文章
- Python 爬取 "王者荣耀.英雄壁纸" 过程中的矛和盾
- python不报错但计算不出结果_excel表格不能用公式怎么办
- (更新时间)2021年3月26日 python基础知识(模块制作)[通俗易懂]
- Python中的取整、取余运算「建议收藏」
- NSGA2 Python实现
- 在树莓派中使用 MicroPython 接入 MQTT
- 2022年最新Python大数据之Python基础【七】参数与管理系统
- Python基础14-内置模块
- 手把手教你用Python破解邻家小妹wifi密码
- python读取oss的psd并上传jpg
- 如何理解python报错信息_csb报错
- python自行实现支付宝证书签名&验签全流程[通俗易懂]
- python读写json_python格式化json
- Anaconda 查看、创建、管理和使用python环境
- 如何设置python的环境变量_anaconda环境变量手动设置
- 用Python画个生日蛋糕为朋友庆生
- Python+OpenCV实时图像处理「建议收藏」
- Python计算中位数_用频率直方图求中位数
- java异或运算符_python 异或
- python识别文字位置_如何利用Python识别图片中的文字