免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2242 | 回复: 3
打印 上一主题 下一主题

python中文件的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-11-17 19:36 |只看该作者 |倒序浏览
使用
    rs=open(‘filename’,‘w')
    打开一个文件,
    然后写入,但是这个写入值大小不定
    rs.close()
    sleep(5)
..#在对这个文件进行其他操作

问题就在这里写入的东西大小不定,rs.close()后需要时间将文件写入filename中,所以才是使用sleep,如何判断文件是否
完全写入,减少sleep的时间??

论坛徽章:
0
2 [报告]
发表于 2012-11-17 21:44 |只看该作者
本帖最后由 crifan 于 2012-11-17 21:45 编辑

用rs.close()之前,先去使用rs.flush(),即可将数据(同步)写回。
然后再close,不用sleep,就可以保证数据是安全写入的了。
刚看了官网的解释:
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.

所以,更按安全的做法是,
  1. rs.flush();
  2. os.fsync(rs);
  3. rs.close();
复制代码
这样,就完全确保数据已经真在写入了。
就不用再sleep了。

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
3 [报告]
发表于 2012-11-18 10:40 |只看该作者
楼上正解.

  1. NAME
  2.        fsync, fdatasync - synchronize a file’s in-core state with storage device

  3. SYNOPSIS
  4.        #include <unistd.h>

  5.        int fsync(int fd);

  6.        int fdatasync(int fd);

  7.    Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

  8.        fsync(): _BSD_SOURCE || _XOPEN_SOURCE
  9.                 || /* since glibc 2.8: */ _POSIX_C_SOURCE >= 200112L
  10.        fdatasync(): _POSIX_C_SOURCE >= 199309L || _XOPEN_SOURCE >= 500

  11. DESCRIPTION
  12.        fsync()  transfers  ("flushes") all modified in-core data of (i.e., modified buffer cache pages for) the file referred to by the file descriptor
  13.        fd to the disk device (or other permanent storage device) so that all changed information can be retrieved even after the system crashed or  was
  14.        rebooted.   This  includes  writing through or flushing a disk cache if present.  The call blocks until the device reports that the transfer has
  15.        completed.  It also flushes metadata information associated with the file (see stat(2)).

  16.        Calling fsync() does not necessarily ensure that the entry in the directory containing the file has also reached disk.   For  that  an  explicit
  17.        fsync() on a file descriptor for the directory is also needed.

  18.        fdatasync()  is  similar  to  fsync(),  but  does not flush modified metadata unless that metadata is needed in order to allow a subsequent data
  19.        retrieval to be correctly handled.  For example, changes to st_atime or st_mtime (respectively, time of last access and time of  last  modifica-
  20.        tion;  see  stat(2))  do  not  require flushing because they are not necessary for a subsequent data read to be handled correctly.  On the other
  21.        hand, a change to the file size (st_size, as made by say ftruncate(2)), would require a metadata flush.

  22.        The aim of fdatasync() is to reduce disk activity for applications that do not require all metadata to be synchronized with the disk.
复制代码

论坛徽章:
0
4 [报告]
发表于 2012-11-18 14:08 |只看该作者
十分感谢两位
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP