python - python: update (source newer than destination or destination missing) copy files
问题描述
Is there a ready tool in python to update copy files? "Update copy" means that a copy should only be made if the source file is newer than the destination or if the destination file is missing, i.e. equivalent of the Linux cp -u
command.
I understand that I could write the code myself by checking for existence of each destination file and by checking the time-stamps of both the source and the destination files and comparing them, but I do not want to "invent the bicycle" (i.e. invent something that has already been invented a long time ago) here if such a tool already exists.
Thank you very much for your help!
解决方案
该distutils
模块可以做到这一点。
例如,要更新 directoryb
中已更改的所有文件 directory a
:
from distutils.dir_util import copy_tree
copy_tree("a", "b", update=1)
不是很有帮助,这将返回其中所有文件的列表,这些文件b
是否a
已更新(也就是说,如果文件x
在两个目录中,即使它不需要更新,它也会在列表中)。
您还可以使用:
from distutils.file_util import copy_file
copy_file('src', 'dst', update=1)
这至少会返回一个有用的元组:
('dst', 1)
1
指示文件已更新,否则0
。(即该值是真实的,但不一定是 a bool
)。
推荐阅读
- sql - 查找连续值长度小于阈值的记录
- sql-server - 在 ColdFusion 中动态创建数据库和数据源
- excel - 当单元格的值为 16、64 或 120 时发送电子邮件
- java - 邮递员响应正文为空
- react-native - 无法在模拟器上安装应用程序并启动它 - React Native
- javascript - puppeteer 网络抓取问题
- python-3.x - 从大量值中找到数据框中的最后一行
- java - 如何在多个 Kubernetes 容器之间共享一个 jar?
- c# - 保存我的对象“学生”并将其加载到我的组合框中的最佳方法是什么?
- c - C数组/堆栈/数组队列?