Python filecmp文件和目录对比模块

简介: ilecmp可以实现文件,目录,遍历子目录的差异对比功能。自带filecmp模块,无需安装。常用方法说明filecmp提供3个操作方法,cmp(单文件对比),cmpfile(多文件对比),dircmp(目录对比).单文件对比,filecmp.cmp(f1,f2[,shallow])f1 f2为文件,相同True,不同False,shallow默认为True,只根据os.stat()方法返回的文件基本信息进行对比。

ilecmp可以实现文件,目录,遍历子目录的差异对比功能。

自带filecmp模块,无需安装。

常用方法说明

filecmp提供3个操作方法,cmp(单文件对比),cmpfile(多文件对比),dircmp(目录对比).

单文件对比,filecmp.cmp(f1,f2[,shallow])

f1 f2为文件,相同True,不同False,shallow默认为True,只根据os.stat()方法返回的文件基本信息进行对比。比如最后访问时间,修改时间,状态改变时间等,会忽略文件内容的对比,当shallow为False时,则os.stat()与文件内容同时进行校验。

<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">1 >>>filrcmp.cmp("/temp/a1","/temp/a2") 2 >>>True 3
4 >>>filrcmp.cmp("/temp/a1","/temp/a3") 5 >>>False</pre>

多文件对比,采用filecmp.cmpfiles(dir1, dir2, common[, shallow])
  比较两个文件夹内指定文件是否相等。参数dir1, dir2指定要比较的文件夹,参数common指定要比较的文件名列表。函数返回包含3个list元素的元组,分别表示匹配、不匹配以及错误的文件列表。错误的文件指的是不存在的文件,或文件被琐定不可读,或没权限读文件,或者由于其他原因访问不了该文件。

  目录对比,通过 filecmp(a,b[,ignore[,hide]])类创建一个目录比较对象
  用于比较文件夹,通过该类比较两个文件夹,可以获取一些详细的比较结果(如只在A文件夹存在的文件列表),并支持子文件夹的递归比较。

<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">dircmp#提供了三个方法用于报告比较的结果:
report():#只比较指定文件夹中的内容(文件与文件夹)
report_partial_closure():#比较文件夹及第一级子文件夹的内容
report_full_closure():#递归比较所有的文件夹的内容</pre>

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">#dircmp还提供了下面这些属性用于获取比较的详细结果
left_list:#左边文件夹中的文件与文件夹列表;
right_list:#右边文件夹中的文件与文件夹列表;
common:#两边文件夹中都存在的文件或文件夹;
left_only:#只在左边文件夹中存在的文件或文件夹;
right_only:#只在右边文件夹中存在的文件或文件夹;
common_dirs:#两边文件夹都存在的子文件夹;
common_files:#两边文件夹都存在的子文件;
common_funny:#两边文件夹都存在的子文件夹;
same_files:#匹配的文件;
diff_files:#不匹配的文件;
funny_files:#两边文件夹中都存在,但无法比较的文件;
subdirs:#将common_dirs 目录映射到新的dircmp对象,格式为字典的类型。</pre>

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 #!/usr/bin/python3
2 #coding:utf-8
3 import os,sys 4 import filecmp 5 import re 6 import shutil 7 '''
8 校验源与备份目录的差异
9 '''
10
11 holderlist = [] 12 def compareme(dir1,dir2): #递归获取更新项函数
13 dircomp = filecmp.dircmp(dir1,dir2) 14 only_in_one = dircomp.left_only #源目录新文件或目录
15 diff_in_one = dircomp.diff_files #不匹配文件,源目录文件已发生变化
16 dirpath = os.path.abspath(dir1) #定义源目录绝对路径
17
18 #将更新文件或目录追加到holderlist
19 [ holderlist.append(os.path.abspath(os.path.join(dir1,x))) for x in only_in_one ] 20 [ holderlist.append(os.path.abspath(os.path.join(dir1,x))) for x in diff_in_one ] 21 if len(dircomp.common_dirs) > 0: #判断是否存在相同子目录,以便递归
22 for item in dircomp.common_dirs: #递归子目录
23 compareme(os.path.abspath(os.path.join(dir1,item)),os.path.abspath(os.path.join(dir2,item))) 24 return holderlist 25
26 def main(): 27 if len(sys.argv) > 2: #输入源目录与备份目录
28 dir1 = sys.argv[1] 29 dir2 = sys.argv[2] 30 else : 31 print('Usage:',sys.argv[0],'datadir backdir') 32 sys.exit() 33 source_files = compareme(dir1,dir2) #对比源目录与备份目录
34 dir1 = os.path.abspath(dir1) #取绝对路径后,后面不会自动加上'/'
35
36 if not dir2.endswith('/'): 37 dir2 = dir2+'/' #备份目录路径加'/'
38
39 dir2 = os.path.abspath(dir2) 40 destination_files = [] 41 createdir_bool = False 42
43 for item in source_files: #遍历返回的差异文件或目录清单
44 destination_dir = re.sub(dir1,dir2,item) #将源目录差异路径清单对应替换成备份目录,即需要在dir2中创建的差异目录和文件
45 destination_files.append(destination_dir) 46 if os.path.isdir(item): #如果差异路径为目录且不存在,则在备份目录中创建
47 if not os.path.exists(destination_dir): 48 os.makedirs(destination_dir) 49 createdir_bool = True #再次调用copareme函数标记
50 if createdir_bool : #重新调用compareme函数,重新遍历新创建目录的内容
51 destination_files = [] 52 source_files = [] 53 source_files = compareme(dir1,dir2) #调用compareme函数
54 for item in source_files: #获取源目录差异路径清单,对应替换成备份目录
55 destination_dir = re.sub(dir1,dir2,item) 56 destination_files.append(destination_dir) 57
58 print('update item:') 59 print(source_files) #输出更新项列表清单
60 copy_pair = zip(source_files,destination_files) #将源目录与备份目录文件清单拆分成元组
61 for item in copy_pair: 62 if os.path.isfile(item[0]): #判断是否为文件,是则进行复制操作
63 shutil.copyfile(item[0],item[1]) 64
65 if name == 'main' : 66 main()</pre>

目录
相关文章
|
21小时前
|
Python Windows
python中的异常与模块
python中的异常与模块
8 1
|
1天前
|
存储 Linux Shell
python移除/删除非空文件夹/目录的最有效方法是什么?
python移除/删除非空文件夹/目录的最有效方法是什么?
7 0
|
1天前
|
数据采集 NoSQL 中间件
python-scrapy框架(四)settings.py文件的用法详解实例
python-scrapy框架(四)settings.py文件的用法详解实例
7 0
|
1天前
|
存储 数据采集 数据库
python-scrapy框架(三)Pipeline文件的用法讲解
python-scrapy框架(三)Pipeline文件的用法讲解
6 0
|
3天前
|
缓存 数据处理 Python
python读取文件到缓存
python读取文件到缓存
9 1
|
4天前
|
存储 数据挖掘 Python
Python技术分享:实现选择文件或目录路径的方法
Python技术分享:实现选择文件或目录路径的方法
15 2
|
4天前
|
前端开发 JavaScript Python
使用Python读取本地行情csv文件,做出web网页画出K线图实现案例
【5月更文挑战第4天】使用Python绘制K线图的步骤:1) 安装pandas, matplotlib和Flask;2) 用pandas读取CSV文件并处理数据;3) 创建Flask应用,渲染包含K线图数据的HTML;4) 编写HTML,使用ECharts库绘制K线图。
23 0
|
10天前
|
JSON 数据格式 Python
Python标准库中包含了json模块,可以帮助你轻松处理JSON数据
【4月更文挑战第30天】Python的json模块简化了JSON数据与Python对象之间的转换。使用`json.dumps()`可将字典转为JSON字符串,如`{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}`,而`json.loads()`则能将JSON字符串转回字典。通过`json.load()`从文件读取JSON数据,`json.dump()`则用于将数据写入文件。
16 1
|
11天前
|
Python
Python实现压缩解压---tarfile模块详解
Python实现压缩解压---tarfile模块详解
|
Python
第43天:Python filecmp&difflib模块
第43天:Python filecmp&difflib模块
129 0