比较源目录与备份目录的差异,并复制

说明:脚本用来确认备份目录与源目录文件是否一致,包括源目录中的新文件或目录、更新文件或目录是否同步成功,可以进行比较复制更新,脚本非双向,如源目录删了文件,脚本不会删备份目录的该文件

使用的模块:通过filecmp模块的left_only、diff_files方法递归获取源目录的更新项,再通过shutil.copyfile、os.makedirs方法对更新进行复制

cp.py     ---->python cp.py  dir1 dir2

#!/usr/bin/env python

#encoding=utf-8
#校验源与备份目录

import os,sys
import filecmp
import re
import shutil  

holderlist=[]   #定义一个对比列表

def compareme(dir1,dir2):      #递归获取更新项函数
        dircomp=filecmp.dircmp(dir1,dir2)  #定义目录对比
        only_in_one=dircomp.left_only  #源目录的新文件或目录,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: #递归子目录
        return holderlist   #返回对比列表
        #print 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.argv[0]为脚本自身
                sys.exit()
        source_files=compareme(dir1,dir2)   #调用上面的compareme函数对比源目录与备份目录
        dir1=os.path.abspath(dir1)  
        if not dir2.endswith('/'):    #判断dir2目录结尾是否有‘/’,如果没有就加上‘/’
                dir2=dir2+'/'
        dir2=os.path.abspath(dir2)  #定义新的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      #再次调用compareme的标记
        if createdir_bool:             #重新调用compareme函数,重新遍历新建的目录的内容
                destination_files=[]
                source_files=[]
                source_files=compareme(dir1,dir2)     #调用compareme函数
                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)  #zip方法将源目录与备份目录揉合成元组
        for item in copy_pair:      #如果判断为文件,则复制到备份目录,方法shutil.copyfile[src,des]
                if os.path.isfile(item[0]):
                        shutil.copyfile(item[0],item[1])

if __name__ == '__main__':

        main()



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值