今天遇到了个问题:

我在执行如下代码时发现,文件只写了一半。也就是说,当文件量过大时,下面的代码是不能保证文件被正确写入的。

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】确保文件写入结束的更多相关文章

  1. 12.python csv文件写入和读出

    import csv headers = ["class", "name", "sex", "height", &quo ...

  2. linux下在用python向文件写入数据时'\n'不起作用

    网上翻看一圈,大家都说利用write写数据换行,在linux下用'\n',windows下利用'\r\n',可是尝试了一下,'\n'在windows底下可换行,在linux底下居然不起作用,最后利用' ...

  3. python读取和写入csv文件

    读取csv文件: def readCsv(): rows=[] with file(r'E:\py\py01\Data\system.csv','rb') as f: reads=csv.reader ...

  4. Python学习笔记——文件写入和读取

    1.文件写入 #coding:utf-8 #!/usr/bin/env python 'makeTextPyhton.py -- create text file' import os ls = os ...

  5. python 利用 ogr 写入shp文件,数据格式

    python 利用 ogr 写入 shp 文件, 定义shp文件中的属性字段(field)的数据格式为: OFTInteger # 整型 OFTIntegerList # 整型list OFTReal ...

  6. 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 ...

  7. python读取与写入csv,txt格式文件

    python读取与写入csv,txt格式文件 在数据分析中经常需要从csv格式的文件中存取数据以及将数据写书到csv文件中.将csv文件中的数据直接读取为dict类型和DataFrame是非常方便也很 ...

  8. python笔记20-yaml文件写入(ruamel.yaml)

    前言 yaml作为配置文件是非常友好的一种格式,前面一篇讲了yaml的一些基础语法和读取方法,本篇继续讲yaml文件写入方法 用yaml模块写入字典嵌套字典这种复杂的数据,会出现大括号{ },不是真正 ...

  9. python中readline判断文件读取结束的方法

    注:内容来自网络 本文实例讲述了python中readline判断文件读取结束的方法.分享给大家供大家参考.具体分析如下: 大家知道,python中按行读取文件可以使用readline函数,下面现介绍 ...

随机推荐

  1. How to step through your code in chrome

    By executing code one line or one function at a time, you can observe changes in the data and in the ...

  2. Java--笔记(6)

    51.jsp的运行周期 jspt生命周期:init .service.destory 除了init只初始化一次外(第一次运行jsp的时候执行),其他 用户端运行JSP时方法都会运行一次. 52.二叉树 ...

  3. android 自定义Style初探---ProgressBar

    系统自带的ProgressBar太丑了,所以我决定自定义一个Style. 原来的Style <?xml version="1.0" encoding="utf-8& ...

  4. 安装MySQL的时候遇到的错误

    这里我安装的是MySQL5.6 我遇到的错误有 (1)Warning: Bison executable not found in PATH 解决办法: yum install bison 原文摘自: ...

  5. 【CF 710F】String Set Queries

    在校内OJ上A了,没有加强制在线的东西..不放链接了. 这道题题意是维护一个字符串集合,支持三种操作: 1.加字符串 2.删字符串 3.查询集合中的所有字符串在给出的模板串中出现的次数 操作数\(m ...

  6. ajax 多个表单值问题,表单序列化加其它表单值

    $.ajax({ type: "post", url: "{:u('cart/totalByCard')}?t="+Math.random(9999), dat ...

  7. .Net 中的反射(反射特性) - Part.3

    反射特性(Attribute) 可能很多人还不了解特性,所以我们先了解一下什么是特性.想想看如果有一个消息系统,它存在这样一个方法,用来将一则短消息发送给某人: // title: 标题:author ...

  8. c语言求数组长度

    在定义数组的函数内 int arr[] = {12.12}; int length; length = ]; 在别的函数中作为引用数据类型引入时,以上方法失效: 解决方法1:再传一个int 类型的长度 ...

  9. 基于dom的xss漏洞原理

    原文:http://www.anying.org/thread-36-1-1.html转载必须注明原文地址最近看到网络上很多人都在说XSS我就借着暗影这个平台发表下自己对这一块的一些认识.其实对于XS ...

  10. 【11-23】mysql学习笔记02

    SQL的历史 SQL是Structed Query Language 的缩写,即”结构化查询语言” SQL原名是 sequel,后来由于法律原因,改名 SQL最早可以追溯到 1974 年,源于 IBM ...