神剑山庄资源网 Design By www.hcban.com

本文实例讲述了Python实现定期检查源目录与备份目录的差异并进行备份功能。分享给大家供大家参考,具体如下:

在项目中,经常要更新文件,在更新之前首先要备份源文件,所以就用到了这个脚本(来自于Python自动化运维这本书),总共有以下几个步骤:

1. 获取要进行比较的两个目录,进行差异比较,把源目录特有的文件或目录、以及和备份目录不同的文件或目录保存到列表中,并且判断目录下面是否还有目录,递归进行保存这些差异文件。
2. 将差异文件列表中文件或目录的路径换成对应的备份路径,进行判断,如果备份路径不存在,就创建目录。
3. 继续对比源目录和新创建的备份目录中的差异文件,把源路径换成备份目录的路径。
4. 然后遍历复制源目录文件到备份目录。

以下是具体的实现代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys
import filecmp
import re
import shutil
holderlist = []
##对应第一个步骤
def compare_me(dir1, dir2):
  dircomp = filecmp.dircmp(dir1, dir2)
  only_in_one = dircomp.left_only
  diff_in_one = dircomp.diff_files
  dirpath = os.path.abspath(dir1)
  [ holderlist.append(os.path.abspath(os.path.join(dir1, x))) for x in only_in_one ]
  [ holderlist.append(os.path.abspath(os.path.join(dir1, x))) for x in diff_in_one ]
  if len(dircomp.common_dirs) > 0:
    for item in dircomp.common_dirs:
      compare_me(os.path.abspath(os.path.join(dir1, item)), os.path.abspath(os.path.join(dir2, item)))
  return holderlist
##对应第二个步骤
def main():
  if len(sys.argv) > 2:
    dir1 = sys.argv[1]
    dir2 = sys.argv[2]
  else:
    print "Usage: ", sys.argv[0], "datadir backupdir"
    sys.exit()
  source_files = compare_me(dir1, dir2)
  dir1 = os.path.abspath(dir1)
  if not dir2.endswith('/'):
    dir2 = dir2 + '/'
  dir2 = os.path.abspath(dir2)
  destination_files = []
  createdir_bool = False
  for item in source_files:
    destination_dir = re.sub(dir1, dir2, item)
    destination_files.append(destination_dir)
    if os.path.isdir(item):
      if not os.path.exists(destination_dir):
        os.makedirs(destination_dir)
        createdir_bool = True
   ##对应第三个步骤
  if createdir_bool:
    destination_files = []
    source_files = []
    source_files = compare_me(dir1, dir2)
    for item in source_files:
      destination_dir = re.sub(dir1, dir2, item)
      destination_files.append(destination_dir)
  ##对应第四个步骤
  print "update item: "
  print source_files
  copy_pair = zip(source_files, destination_files)
  print "copy_pair is %s" % copy_pair
  for item in copy_pair:
    print "item is %s, %s" % (item[0], item[1])
    if os.path.isfile(item[0]):
      shutil.copyfile(item[0], item[1])
if __name__ == '__main__':
  main()

最后根据需要,可以设定一个定时检查,进行自动同步源目录和备份目录,让其保持一致性。

PS:这里再为大家推荐一款功能相似的在线工具供大家参考使用:

在线文本比较工具:
http://tools.jb51.net/aideddesign/txt_diff

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

标签:
Python,定期检查,源目录,备份目录,差异,备份

神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网 免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com

评论“Python实现定期检查源目录与备份目录的差异并进行备份功能示例”

暂无Python实现定期检查源目录与备份目录的差异并进行备份功能示例的评论...

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?