仅在Python中源比目标新时才如何复制文件?
问题内容:
我正在编写一个脚本,将已编译的文件从一个位置复制到另一个位置。
我目前所拥有的是这样的:
import os
import shutil
shutil.copy2 (src, dst)
#... many more shutil.copy commands
#src is a filename string
#dst is the directory where the file is to be copied
我的问题是,许多要复制的文件都是大文件,并不是每个编译周期都重新编译所有文件。理想情况下,我只想在此脚本中复制更改的文件。有什么办法可以做到吗?
问题答案:
如果您足够的话,您可以利用文件修改时间:
# If more than 1 second difference
if os.stat(src).st_mtime - os.stat(dest).st_mtime > 1:
shutil.copy2 (src, dst)
或调用rsync之类的同步工具。