![图片[1]-python比较两个目录的文件是否相同-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/5dc3b5c1021a0350.jpg)
有时候我们要对比两份配置文件是不是一样,或者比较两个文本是否异样,可以使用linux命令行工具diff a_file b_file,但是输出的结果读起来不是很友好。这时候使用python的标准库difflib就能满足我们的需求。
下面这个脚本使用了difflib和argparse,argparse用于解析我们给此脚本传入的两个参数(即两份待比较的文件),由difflib执行比较,比较的结果放到了一个html里面,只要找个浏览器打开此html文件,就能直观地看到比较结果,两份文件有差异的地方会高亮显示出来。
以python2.7为例,compare_two_files.py程序正文:
#!/bin/envpython
#-*-coding:utf-8-*-
importdifflib
importsys
importargparse
#读取建表语句或配置文件
defread_file(file_name):
try:
file_desc=open(file_name,'r')
#读取后按行分割
text=file_desc.read().splitlines()
file_desc.close()
returntext
exceptIOErroraserror:
print'ReadinputfileError:{0}'.format(error)
sys.exit()
#比较两个文件并把结果生成一份html文本
defcompare_file(file1,file2):
iffile1==""orfile2=="":
print'文件路径不能为空:第一个文件的路径:{0},第二个文件的路径:{1}.'.format(file1,file2)
sys.exit()
else:
print"正在比较文件{0}和{1}".format(file1,file2)
text1_lines=read_file(file1)
text2_lines=read_file(file2)
diff=difflib.HtmlDiff()#创建HtmlDiff对象
result=diff.make_file(text1_lines,text2_lines)#通过make_file方法输出html格式的对比结果
#将结果写入到result_comparation.html文件中
try:
withopen('result_comparation.html','w')asresult_file:
result_file.write(result)
print"0==}==========>SuccessfullyFinished\n"
exceptIOErroraserror:
print'写入html文件错误:{0}'.format(error)
if__name__=="__main__":
#Todefinetwoargumentsshouldbepassedin,andusage:-f1fname1-f2fname2
my_parser=argparse.ArgumentParser(description="传入两个文件参数")
my_parser.add_argument('-f1',action='store',dest='fname1',required=True)
my_parser.add_argument('-f2',action='store',dest='fname2',required=True)
#retrieveallinputarguments
given_args=my_parser.parse_args()
file1=given_args.fname1
file2=given_args.fname2
compare_file(file1,file2)
【待比较的文件】
两份文件分别是old_ddl_file和new_ddl_file,内容分别是:
old_ddl_file文件内容
CREATEEXTERNALTABLEraw_tags( p0stringCOMMENT‘uid’, p3stringCOMMENT‘tagname,e.g.news,games,fairs,shoopingURL’, p4stringCOMMENT‘e.g.0,Games’, p11intCOMMENT‘gender’, dtstringCOMMENT‘date,like26/6/2017’, actionstringCOMMENT‘clickmodule,click_taghead_link,clicklink’) CLUSTEREDBY( dt) INTO4BUCKETS ROWFORMATDELIMITED FIELDSTERMINATEDBY‘,’ STOREDASINPUTFORMAT ‘org.apache.hadoop.mapred.TextInputFormat’ OUTPUTFORMAT ‘org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat’ LOCATION ‘hdfs://hdfs-ha/apps/hive/warehouse/ksai.db/raw_tags’ TBLPROPERTIES( ‘numFiles’=’1’, ‘numRows’=’0’, ‘rawDataSize’=’0’, ‘totalSize’=’70575510’, ‘transient_lastDdlTime’=’1500469448’)
new_ddl_file文件内容
CREATEEXTERNALTABLEraw_tags(
p0stringCOMMENT‘uid’,
p3stringCOMMENT‘tagname,e.g.news,games,fairs,shoopingURL’,
p4stringCOMMENT‘e.g.0,Games’,
p11intCOMMENT‘gender’,
dtstringCOMMENT‘date,like26/6/2017’,
actionstringCOMMENT‘clickmodule,click_taghead_link,clicklink’)
ROWFORMATDELIMITED
FIELDSTERMINATEDBY‘,’
STOREDASINPUTFORMAT
‘org.apache.hadoop.mapred.TextInputFormat’
OUTPUTFORMAT
‘org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat’
LOCATION
‘hdfs://hdfs-ha/apps/hive/warehouse/ksai.db/raw_tags’
TBLPROPERTIES(
‘COLUMN_STATS_ACCURATE’=’{\”BASIC_STATS\”:\”true\”}’,
‘numFiles’=’0’,
‘numRows’=’0’,
‘rawDataSize’=’0’,
‘totalSize’=’0’,
‘transient_lastDdlTime’=’1521546069’)
肉眼很难看出来区别吧?
【执行结果】
那么就使用上面的脚本来比较,在linux命令行的使用方法 python -f1 file1 -f2 file2 也就是:
python compare_two_files.py -f1 old_ddl_file -f2 new_ddl_file

再把运行结果产生的html文件下载到本地,用任一种浏览器打开即可,如截图:

运行结果:
![图片[4]-python比较两个目录的文件是否相同-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/1573106861792893.jpg)
使用浏览器查看html文件,可以看到,里面给出了各种颜色标注的图例说明,一目了然。
原文来自:https://www.py.cn© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END



















































暂无评论内容