Python自动化运维——文件与目录差异对比

Infi-chu:

http://www.cnblogs.com/Infi-chu/

模块:filecmp

安装:Python版本大于等于2.3默认自带

功能:实现文件、目录、遍历子目录的差异

常用方法:

1.单文件对比(cmp):

  1. 采用filecmp.cmp(f1,f2[,shallow]) 方法,[]可空
  2. 比较f1和f2文件,相同返回True,否则False
  3. shallow默认为True,意思是只根据os.stat() 方法返回的文件基本信息进行对比,即文件基本信息。当为False时,os.stat() 与文件内容同时进行校验

2.多文件对比(cmpfiles):

  1. 采用filecmp.cmpfiles(dir1,dir2,common[,shallow]) 方法
  2. 对比dir1和dir2目录给定的文件清单。
  3. 此方法返回文件名的三个列表:匹配、不匹配、错误
  4. 匹配:包含匹配的文件的列表
  5. 不匹配:与匹配相反
  6. 错误:目录不存在文件、不具备读权限或其他原因导致的不能比较的文件清单

3.目录对比(dircmp):

  1. 使用dircmp(a,b[,ignore[,hide]]) 类创建一个目录比较对象
  2. a和b是参加比较的目录名;
  3. ignore代表文件名忽略的列表,默认为['RCS','CVS','tags'];
  4. hide代表隐藏的列表,默认为[os.curdir,os.pardir]。
  5. dircmp类可以获得目录较为详细的信息
  6. dircmp提供了三个输出报告的方法:
    1. report(),比较当前指定目录中的内容
    2. report_partial_closure(),比较当前指定目录及第一级子目录中的内容
    3. report_full_closure(),递归比较所有指定目录的内容
  7. dircmp为输出提供了更加详细的比较结果,提供了如下属性:
    1. left,左目录,如上述的a
    2. right,右目录,如上述的b
    3. left_list,左目录中的文件及目录列表
    4. right_list,右目录中的文件及目录列表
    5. common,两边目录共同存在的文件或目录
    6. left_only,只在左目录中的文件或目录
    7. right_only,只在右目录中的文件或目录
    8. common_dirs,两边目录都存在的子目录
    9. common_files,两边目录都存在的子文件
    10. common_funny,两边目录都存在的子目录(不同目录类型或os.stat()记录的错误)
    11. same_files,匹配相同的文件
    12. diff_files,不匹配的文件
    13. funny_files,两边目录中都存在,但无法比较的文件
    14. subdirs,将common_dirs目录名映射到新的dircmp对象,格式是字典类型

比较dir1和dir2的目录差异:(先自定义目录下的文件与目录,再使用)

 1 #import filecmp
 2 
 3 #a='/home/test/filecmp/dir1' # 左目录
 4 
 5 #b='/home/test/filecmp/dir2' # 右目录
 6 
 7 #dir_obj=filecmp.dircmp(a,b)
 8 
 9 #dir_obj.report()
10 
11 #dir_obj.report_partial_closure()
12 
13 #dir_obj.report_full_closure()
14 
15 #print('left_list:'+str(dir_obj.left_list))
16 
17 #print('right_list:'+str(dir_obj.right_list))
18 
19 #print('common'+str(dir_obj.common))
20 
21 # 根据上述属性,以此类推

校验源目录与备份目录的差异:

  1 #import os
  2 
  3 #import sys
  4 
  5 #import filecmp
  6 
  7 #import re
  8 
  9 #import shutil
 10 
 11 #
 12 
 13 #holderlist=[]
 14 
 15 #
 16 
 17 #def compare(dir1,dir2): # 递归获取更新项
 18 
 19 # dircomp=filecmp.dircmp(dir1,dir2)
 20 
 21 # only_in_one=dircomp.left_only # 源目录更新文件或目录
 22 
 23 # diff_in_one=dircomp.diff_files # 不匹配文件,源目录文件已发生变化
 24 
 25 # dirpath=os.path.abspath(dir1) # 定义源目录绝对路径
 26 
 27 # # 将更新文件名或目录追加到holderlist
 28 
 29 # [holderlist.append(os.path.abspath(os.path.join(dir1,x))) for x in only_in_one]
 30 
 31 # [holderlist.append(os.path.abspath(os.path.join(dir1,x))) for x in diff_in_one]
 32 
 33 # if len(dircomp.common_dirs) > 0: # 判断目录是否存在相同子目录,便于递归
 34 
 35 # for item in dircomp.common_dirs: # 递归子目录
 36 
 37 # compareme(os.path.abspath(os.path.join(dir1,item)), \
 38 
 39 # os.path.abspath(os.path.join(dir2,item)))
 40 
 41 # return holderlist
 42 
 43 #
 44 
 45 #def main:
 46 
 47 # if len(sys.argv) > 2: # 要求输入源目录与备份目录
 48 
 49 # dir1=sys.argv[1]
 50 
 51 # dir2=sys.argv[2]
 52 
 53 # else:
 54 
 55 # print('使用:',sys.argv[0],'源目录 备份目录')
 56 
 57 # sys.exit()
 58 
 59 # source_files=compareme(dir1,dir2) # 对比两个目录
 60 
 61 # dir1=os.path.abspath(dir1)
 62 
 63 # if not dir2.endswith('/'):
 64 
 65 # dir2=dir2+'/' # 备份目录路径加'/' 符
 66 
 67 # dir2=os.path.abspath(dir2)
 68 
 69 # destination_files=[]
 70 
 71 # createdir_bool=False
 72 
 73 #
 74 
 75 # for item in source_files: # 遍历返回的差异文件或目录清单
 76 
 77 # destination_dir=re.sub(dir1,dir2,item) # 将源目录差异路径清单对应替换成备份目录
 78 
 79 # destination_files.append( destination_dir)
 80 
 81 # if os.path.isdir(item): # 如果差异路径为目录且不存在,则在备份目录中创建
 82 
 83 # if not os.path.exists(destination_dir):
 84 
 85 # os.makedirs(destination_dir)
 86 
 87 # createdir_bool=True
 88 
 89 # if createdir_bool: # 重新调用,重新遍历新创建目录的内容
 90 
 91 # destination_files=[]
 92 
 93 # source_files=[]
 94 
 95 # source_files=compareme(dir1,dir2)
 96 
 97 # for item in source_files: # 获取源目录差异路径清单,对应替换成备份目录
 98 
 99 # destination_dir=re.sub(dir1,dir2,item)
100 
101 # destination_files.append(destination_dir)
102 
103 # print('更新项:')
104 
105 # print(source_files) # 输出更新项列表清单
106 
107 # copy_pair=zip(source_files,destination_files) # 将源目录与备份目录文件清单差分成元组
108 
109 # for item in copy_pair:
110 
111 # if os.path.isfile(item[0]): # 判断是否是文件,然后进行复制操作
112 
113 # shutil.copyfile(item[0],item[1])
114 
115 #
116 
117 #if __name__='__main__':
118 
119 # main()

 

转载于:https://www.cnblogs.com/Infi-chu/p/8120276.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值