Python编程实现两个文件夹里文件的对比功能示例【包含内容的对比】
本文实例讲述了Python编程实现两个文件夹里文件的对比功能。分享给大家供大家参考,具体如下:
#-*-coding:utf-8-*-
#===============================================================================
#目录对比工具(包含子目录),并列出
#1、A比B多了哪些文件
#2、B比A多了哪些文件
#3、二者相同的文件:文件大小相同VS文件大小不同(Size相同文件不打印:与Size不同文件显示未排序)
#===============================================================================
importos,time,difflib
AFILES=[]#EE
BFILES=[]#SVN
COMMON=[]#EE&SVN
defgetPrettyTime(state):
returntime.strftime('%y-%m-%d%H:%M:%S',time.localtime(state.st_mtime))
#defgetpathsize(dir):#获取文件大小的函数,未用上,仅供学习.故注释掉
#size=0
#forroot,dirs,filesinos.walk(dir):
##root:目录:str如:C:\CopySVN\SystemObject\TopoProcedure\Built-in\
##dirs:目录名称:列表:如['Parsers']
##files:名称:列表:如['011D0961FB42416AA49D5E82945DE7E9.og',...]
##file:目录:str,如011D0961FB42416AA49D5E82945DE7E9.og
#forfileinfiles:
#path=os.path.join(root,file)
#size=os.path.getsize(path)
#returnsize
defdirCompare(apath,bpath):
afiles=[]
bfiles=[]
forroot,dirs,filesinos.walk(apath):
forfinfiles:
afiles.append(root+"\\"+f)
forroot,dirs,filesinos.walk(bpath):
forfinfiles:
bfiles.append(root+"\\"+f)
#sizeB=os.path.getsize(root+"\\"+f)此处定义的size无法在commonfiles进行比较.(A,B在各自的循环里面)
#去掉afiles中文件名的apath(拿A,B相同的路径\文件名,做成集合,去找交集)
apathlen=len(apath)
aafiles=[]
forfinafiles:
aafiles.append(f[apathlen:])
#去掉bfiles中文件名的bpath
bpathlen=len(bpath)
bbfiles=[]
forfinbfiles:
bbfiles.append(f[bpathlen:])
afiles=aafiles
bfiles=bbfiles
setA=set(afiles)
setB=set(bfiles)
#print('%$%'+str(len(setA)))
#print('%%'+str(len(setB)))
commonfiles=setA&setB#处理共有文件
#print("===============Filewithdifferentsizein'",apath,"'and'",bpath,"'===============")
#将结果输出到本地
#withopen(os.getcwd()+'diff.txt','w')asdi:
#di.write("===============Filewithdifferentsizein'",apath,"'and'",bpath,"'===============")
forfinsorted(commonfiles):
sA=os.path.getsize(apath+"\\"+f)
sB=os.path.getsize(bpath+"\\"+f)
ifsA==sB:#共有文件的大小比较
#pass#print(f+"\t\t"+getPrettyTime(os.stat(apath+"\\"+f))+"\t\t"+getPrettyTime(os.stat(bpath+"\\"+f)))
#以下代码是处理大小一致,但是内容可能不一致的情况
#print("insa=sb")
#print(os.getcwd())
saf=[]
sbf=[]
sAfile=open(apath+"\\"+f)
iter_f=iter(sAfile)
forlineiniter_f:
saf.append(line)
sAfile.close()
sBfile=open(bpath+"\\"+f)
iter_fb=iter(sBfile)
forlineiniter_fb:
sbf.append(line)
sBfile.close()
saf1=sorted(saf)
sbf1=sorted(sbf)
if(len(saf1)!=len(sbf1)):
withopen(os.getcwd()+'\\comment_diff.txt','a')asfp:
print(os.getcwd())
fp.write(apath+"\\"+f+"linessizenotequal"+bpath+"\\"+f+'\n')
else:
foriinrange(len(saf1)):
#print("intopre")
if(saf1[i]!=sbf1[i]):
print('intocommont')
withopen(os.getcwd()+'\\comment_diff.txt','a')asfp1:
fp1.write(apath+"\\"+f+"contentnotequal"+bpath+"\\"+f+'\n')
break
else:
withopen(os.getcwd()+'\\diff.txt','a')asdi:
di.write("FileName=%sEEresourcefilesize:%d!=SVNfilesize:%d"%(f,sA,sB)+'\n')
#print("FileName=%sEEresourcefilesize:%d!=SVNfilesize:%d"%(f,sA,sB))
#处理仅出现在一个目录中的文件
onlyFiles=setA^setB
aonlyFiles=[]
bonlyFiles=[]
forofinonlyFiles:
ifofinafiles:
aonlyFiles.append(of)
elifofinbfiles:
bonlyFiles.append(of)
#print("######################EEresourceONLY###########################")
#print("#onlyfilesin",apath)
forofinsorted(aonlyFiles):
withopen(os.getcwd()+'\\EEonly.txt','a')asee:
ee.write(of+'\n')
#print(of)
#print("*"*20+"SVNONLY+"+"*"*20)
#print("#onlyfilesin",bpath)
forofinsorted(bonlyFiles):
withopen(os.getcwd()+'\\svnonly.txt','a')assvn:
svn.write(of+'\n')
#print(of)
if__name__=='__main__':
FolderEE='D:\\search\\bb\\ObjectGroup-Copy\\ObjectGroup\\Built-in'
FolderSVN='D:\\search\\bb\\ObjectGroup\\ObjectGroup\\Built-in'
dirCompare(FolderEE,FolderSVN)
print("done!")
PS:这里再为大家推荐一款功能相似的在线工具供大家参考使用:
在线文本比较工具:
http://tools.jb51.net/aideddesign/txt_diff
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。