当输出相同时,不会覆盖文件的简单方法 [英] Easy way of not overwriting file when output is the same

查看:223
本文介绍了当输出相同时,不会覆盖文件的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++代码生成器在Python中生成许多源文件。大多数时候,只有一个文件更改,但是因为生成器会重新生成所有文件,因此它们都会重新生成。有没有办法让Python不覆盖文件,或者让cmak使用校验和来查看需要重建的内容,而不仅仅是使用文件日期?

I have a C++ code generator in Python that generates many source files. Most of the time, only one file changes, but because the generators regenerates all of the files, they are all rebuilt. Is there a way to either get Python to not overwrite the files, or else to get cmak to use a checksum to see what needs to be rebuilt rather than just using the file date?

我在想像这样在Python中很容易:如果我可以用open('blah')替换

I was thinking something like this would be easy in Python: If I could replace

with open('blah', 'w') as f:

with open_but_only_overwrite_if_total_output_is_different('blah', 'w') as f:


推荐答案

结合尼尔G Petr Viktorin gecco joel3000

import contextlib
@contextlib.contextmanager
def write_on_change(filename):
    with tempfile.NamedTemporaryFile(delete=False) as f:
        yield f
        tempname = f.name
    try:
        overwrite = not filecmp.cmp(tempname,filename)
    except (OSError,IOError):
        overwrite = True
    if overwrite:
        shutil.copyfile(tempname,filename)
    os.unlink(tempname)

添加(希望改进):


  • shutil.copyfile code> into
    filename ,同时保留像文件权限和文件
    所有权的元数据。

  • filecmp.cmp 检查文件大小
    并返回 False 如果大小不匹配。这可能是一个很好的
    加速,如果文件很大,一个有东西附加到
    结束。它还会读取并比较 bufsize = 8 * 1024 字节一次,
    ,而不是一次一行。 bufsize 通常会大于
    行,这样会减少读取次数。

  • shutil.copyfile only copies the contents of tempname into filename, while preserving metadata like file permissions and file ownership.
  • filecmp.cmp checks the size of the files and returns False if the sizes don't match. That could be a nice speedup if the files are large and one has stuff appended to the end. It also reads and compares bufsize = 8*1024 bytes at a time, instead of lines at a time. bufsize will generally be bigger than a line, which would result in fewer reads.

这篇关于当输出相同时,不会覆盖文件的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆