python 写入文件
2016-06-16 21:05
543 查看
python,用:?1rs=open(‘filename’,‘w')打开文件后,然后写入一定量的数据,再去关闭:?12rs.close()sleep(5)但是问题在于,每次写入文件的数据,是不固定的,所以,不知道sleep多长时间,才能确保真正数据已经写入了。 【解决过程】1.实际上,这个问题,是涉及到文件的缓存,操作系统的缓存方面的内容。而对此,文件级别的缓存,已经有对应的函数:file.flush()Flush the internal buffer, like stdio‘s fflush(). This
may be a no-op on some file-like objects.Noteflush() 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 do os.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.所以,相关的,正确的,完整的代码,就是:?12345678910import 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 safefileObj.close();
may be a no-op on some file-like objects.Noteflush() 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 do os.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.所以,相关的,正确的,完整的代码,就是:?12345678910import 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 safefileObj.close();
相关文章推荐
- 《推荐系统》基于图的推荐算法
- BinarySortTree java python leetcode
- Python中pip的安装
- Python capitalize()方法
- python和编码
- python学习笔记7——列表中整数乘积末尾0的问题
- python微教程---第一季(第一集)----python的安装
- python最简单发送邮件
- python2.7安装OpenCV
- Python程序-打印单词的菱形字符图案
- pip2 与 pip3
- python WEB UI自动化在日期框中动态输入当前日期
- python实现东南大学体育馆自动预约
- python之web业务质量检测
- 从局部变量和全局变量开始全面解析Python中变量的作用域
- Python, 数据抓取
- BuildBinaryTreeby前序遍历和中序遍历 python java leetcode
- Python的安装和环境配置
- 实例讲解Python中global语句下全局变量的值的修改
- 将Mercurial整合到Flask项目中