使用Python备份目录和子目录;使用 os.walk 或 filecmp.dircmp 或其他东西
我是一个蟒蛇新手。 我的问题是我应该使用什么方法来设置文件/目录备份例程,如下所述(os.walk 或 filecmp.dircmp 或其他)。
我想设置一个备份例程如下:
每天晚上,我想将“bakup_dir_a1”(及其所有子目录)制作为“local_dir_a”(及其所有子目录)的镜像;但是,每个晚上。 。 。
首先,我想将 local_dir_a(及其所有子目录)与 bakup_dir_a1(及其所有子目录)进行比较,以找出差异。
接下来,我想在 bakup_dir_a1 (及其所有子目录)中创建一个文件列表(包括文件名的完整路径),该列表将被从 local_dir_a (及其所有子目录)复制的较新文件以及相应的最后 -新文件和旧文件的修改日期;
接下来,我想在 bakup_dir_a1 (及其所有子目录)中创建一个文件列表(包括文件名的完整路径),该列表将简单地从 bakup_dir_a (及其所有子目录)中删除;
接下来,我想在 bakup_dir_a2 中创建一个存档(.rar 或 .zip),其中包含第 1 段中标识的所有文件的副本。 3 和没有。
最后,我将执行上面第 1 段中描述的镜像。
我花了一些时间尝试学习如何使用 os.walk 和 filecmp.dircmp。
我怀疑 os.walk 可能是更适合我的目的的设备。
任何建议将不胜感激。 谢谢, 马克
I am a python newbie.
My question is what approach I should use to set up a file/directory backup routine, as described below (os.walk or filecmp.dircmp, or something else).
I want to set up a backup routine as follows:
Every night, I want to make "bakup_dir_a1" (and all its subdirectories) into a mirror of "local_dir_a" (and all its subdirectories); But, each night . . .
First, I want to compare local_dir_a (and all its subdirectories) to bakup_dir_a1 (and all its subdirectories), to identify differences.
Next, I want to create a list of files (full path including filename) in bakup_dir_a1 (and all its subdirectories), that will be replaced by newer files copied from local_dir_a (and all its subdirectories), and the respective last-modified dates of the newer and the older files;
Next, I want to create a list of files (full path including filename) in bakup_dir_a1 (and all its subdirectories), that will be simply deleted from bakup_dir_a (and all its subdirectories);
Next, I want to create an archive (.rar or .zip) in bakup_dir_a2 containing a copy of all the files identified in paragraphs no. 3 and no. 4 above.
Lastly, I will execute the mirroring described in paragraph 1 above.
I've spent some time trying to learn how to work with os.walk and filecmp.dircmp.
I suspect that os.walk might be the better device to use for my purposes.
Any suggestions would be much appreciated.
Thanks,
Marc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一步,查看 shutil 模块,从http://docs.python.org/library/shutil.html#shutil.copytree 个
对于第二 步骤, filecmp.dircmp 是一个合理的选择。
第五步,查看 tarfile 模块 和 zipfile 模块。
For the first step, take a look at the shutil module, starting with http://docs.python.org/library/shutil.html#shutil.copytree
For the second step, filecmp.dircmp is a reasonable choice.
For the fifth step, take a look at the archiving options in the tarfile module and zipfile module.