【python】确保文件写入结束
今天遇到了个问题:
我在执行如下代码时发现,文件只写了一半。也就是说,当文件量过大时,下面的代码是不能保证文件被正确写入的。
fd = open('test.txt','w')
fd.write("a lot of thing")
fd.close()
解决办法:
来源:http://www.crifan.com/python_after_write_file_then_do_not_know_how_long_to_sleep_is_safe_close/
1.实际上,这个问题,是涉及到文件的缓存,操作系统的缓存方面的内容。
而对此,文件级别的缓存,已经有对应的函数:
- file.flush()
Flush the internal buffer, like stdio‘s fflush(). This may be a no-op on some file-like objects.
Note
flush() does not necessarily write the file’s data to disk. Use flush()followed by os.fsync() to ensure this behavior.
去保证数据的写回了.
2.而且另外,上面也提到了,如果想要真正能够确保数据的确已经写回了,可以使用:
- os.fsync(fd)
Force write of file with filedescriptor fd to disk. On Unix, this calls the native fsync() function; on Windows, the MS _commit() function.
If you’re starting with a Python file object f, first do f.flush(), and then doos.fsync(f.fileno()), to ensure that all internal buffers associated with f are written to disk.
Availability: Unix, and Windows starting in 2.2.3.
所以,相关的,正确的,完整的代码,就是:
import os;
fileObj=open('filename', 'w');
#write data into fileObj here
#first do file flush()
fileObj.flush();
#then os fsync()
os.fsync(fileObj);
#then close is safe
fileObj.close();
【python】确保文件写入结束的更多相关文章
- 12.python csv文件写入和读出
		import csv headers = ["class", "name", "sex", "height", &quo ... 
- linux下在用python向文件写入数据时'\n'不起作用
		网上翻看一圈,大家都说利用write写数据换行,在linux下用'\n',windows下利用'\r\n',可是尝试了一下,'\n'在windows底下可换行,在linux底下居然不起作用,最后利用' ... 
- python读取和写入csv文件
		读取csv文件: def readCsv(): rows=[] with file(r'E:\py\py01\Data\system.csv','rb') as f: reads=csv.reader ... 
- Python学习笔记——文件写入和读取
		1.文件写入 #coding:utf-8 #!/usr/bin/env python 'makeTextPyhton.py -- create text file' import os ls = os ... 
- python 利用 ogr 写入shp文件,数据格式
		python 利用 ogr 写入 shp 文件, 定义shp文件中的属性字段(field)的数据格式为: OFTInteger # 整型 OFTIntegerList # 整型list OFTReal ... 
- python之创建文件写入内容
		https://www.cnblogs.com/evablogs/p/7096686.html 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 ... 
- python读取与写入csv,txt格式文件
		python读取与写入csv,txt格式文件 在数据分析中经常需要从csv格式的文件中存取数据以及将数据写书到csv文件中.将csv文件中的数据直接读取为dict类型和DataFrame是非常方便也很 ... 
- python笔记20-yaml文件写入(ruamel.yaml)
		前言 yaml作为配置文件是非常友好的一种格式,前面一篇讲了yaml的一些基础语法和读取方法,本篇继续讲yaml文件写入方法 用yaml模块写入字典嵌套字典这种复杂的数据,会出现大括号{ },不是真正 ... 
- python中readline判断文件读取结束的方法
		注:内容来自网络 本文实例讲述了python中readline判断文件读取结束的方法.分享给大家供大家参考.具体分析如下: 大家知道,python中按行读取文件可以使用readline函数,下面现介绍 ... 
随机推荐
- CSS3系列之3D制作
			一.序 博主最近这些天,突发奇想的想研究一下CSS3的东西,从而提升一下CSS的能力,在学习的过程中发现其实CSS3是一个挺复杂的东西,深入的研究,你可能会涉及到初中的光学理论来帮助理解一些概念,同时 ... 
- JavaScript模板引擎artTemplate.js——template.compile()方法
			template.compile(source, options) source:必传,渲染模板的内容. options:可选,通常不传.(其实是我还没研究明白) return:一个渲染函数. 示例如 ... 
- jprofiler_监控远程linux服务器的JVM进程(实践)
			几天前写了一篇文章,jprofiler_监控远程linux服务器的tomcat进程(实践),介绍了使用jprofiler怎样监控远程linux的tomcat进程,这两天想了想,除了可以监控tomcat ... 
- 特征检测(feature detection)的一些资料
			FAST特征点: http://blog.csdn.net/hujingshuang/article/details/46898007 BRIEF特征描述子: http://blog.csdn.net ... 
- java高新技术-可变参数与OverLoad相关面试题分析
			可变参数 可变参数的特点: 只能出现在参数列表的最后: ...位于变量类型和变量名之间,前后有无空格都可以: 调用可变参数的方法时,编译器为该可变参数隐含创建一个数组,在方法中以数组的形式访问可变参数 ... 
- transition
			transition: all 所有属性都将获得过渡效果. property 定义应用过渡效果的 CSS 属性名称列表,列表以逗号分隔 transition-duration 规定完成过渡效果需要多少 ... 
- C#的继承
			什么是继承:继承是允许重用现有类去创建新类的过程.分类的原则是一个类派生出来的子类具有这个类的所有非私有的属性. 1.继承C#中的类:C#不支持多重继承,C#类始终继承自一个基类(如果未在声明中指定一 ... 
- primefaces 查询 点击按钮 加载 动画 ajax loader
			只要在/WEB-INF/template.xhtml中body 里面加入: <ui:insert name="status"> <p:ajaxStatus sty ... 
- Transmission : 如何在Fedora下使用BT下载
			先讲讲BT协议的名词: Glossary of BitTorrent terms Transmission 介绍 BT下载客户端 特点: 支持BT下载(.torrent 种子) 或者 磁链( magn ... 
- The RAII Programming Idiom
			https://www.hackcraft.net/raii/ https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization 
