Python編程實(shí)現(xiàn)兩個(gè)文件夾里文件的對(duì)比功能示例【包含內(nèi)容的對(duì)比】
本文實(shí)例講述了Python編程實(shí)現(xiàn)兩個(gè)文件夾里文件的對(duì)比功能。分享給大家供大家參考,具體如下:
#-*-coding:utf-8-*- #=============================================================================== # 目錄對(duì)比工具(包含子目錄 ),并列出 # 1、A比B多了哪些文件 # 2、B比A多了哪些文件 # 3、二者相同的文件:文件大小相同 VS 文件大小不同 (Size相同文件不打印:與Size不同文件顯示未排序) #=============================================================================== import os, time,difflib AFILES = [] #EE BFILES = [] #SVN COMMON = [] #EE & SVN def getPrettyTime(state): return time.strftime('%y-%m-%d %H:%M:%S', time.localtime(state.st_mtime)) # def getpathsize(dir): #獲取文件大小的函數(shù),未用上,僅供學(xué)習(xí).故注釋掉 # size=0 # for root, dirs, files in os.walk(dir): # #root:目錄:str 如: C:\CopySVN\SystemObject\TopoProcedure\Built-in\ # #dirs:目錄名稱:列表: 如 ['Parsers'] # #files:名稱:列表: 如 ['011D0961FB42416AA49D5E82945DE7E9.og',...] # #file:目錄:str, 如 011D0961FB42416AA49D5E82945DE7E9.og # for file in files: # path = os.path.join(root,file) # size = os.path.getsize(path) # return size def dirCompare(apath,bpath): afiles = [] bfiles = [] for root, dirs , files in os.walk(apath): for f in files: afiles.append(root + "\\" + f) for root, dirs , files in os.walk(bpath): for f in files: bfiles.append(root + "\\" + f) #sizeB = os.path.getsize(root + "\\" + f) 此處定義的size無法在commonfiles進(jìn)行比較. (A,B在各自的循環(huán)里面) # 去掉afiles中文件名的apath (拿A,B相同的路徑\文件名,做成集合,去找交集) apathlen = len(apath) aafiles = [] for f in afiles: aafiles.append(f[apathlen:]) # 去掉bfiles中文件名的bpath bpathlen = len(bpath) bbfiles = [] for f in bfiles: 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 ("===============File with different size in '", apath, "' and '", bpath, "'===============") #將結(jié)果輸出到本地 #with open(os.getcwd()+'diff.txt','w') as di: #di.write("===============File with different size in '", apath, "' and '", bpath, "'===============") for f in sorted(commonfiles): sA=os.path.getsize(apath + "\\" + f) sB=os.path.getsize(bpath + "\\" + f) if sA==sB: #共有文件的大小比較 #pass #print (f + "\t\t" + getPrettyTime(os.stat(apath + "\\" + f)) + "\t\t" + getPrettyTime(os.stat(bpath + "\\" + f))) #以下代碼是處理大小一致,但是內(nèi)容可能不一致的情況 #print("in sa=sb") #print(os.getcwd()) saf=[] sbf=[] sAfile=open(apath + "\\" + f) iter_f=iter(sAfile) for line in iter_f: saf.append(line) sAfile.close() sBfile=open(bpath + "\\" + f) iter_fb=iter(sBfile) for line in iter_fb: sbf.append(line) sBfile.close() saf1=sorted(saf) sbf1=sorted(sbf) if(len(saf1)!=len(sbf1)): with open(os.getcwd()+'\\comment_diff.txt','a') as fp: print(os.getcwd()) fp.write(apath + "\\" + f+" lines size not equal "+bpath + "\\" + f+'\n') else: for i in range(len(saf1)): #print("into pre") if(saf1[i]!=sbf1[i]): print('into commont') with open(os.getcwd()+'\\comment_diff.txt','a') as fp1: fp1.write(apath + "\\" + f+" content not equal "+bpath + "\\" + f+'\n') break else: with open (os.getcwd()+'\\diff.txt','a') as di: di.write("File Name=%s EEresource file size:%d != SVN file size:%d" %(f,sA,sB)+'\n') #print ("File Name=%s EEresource file size:%d != SVN file size:%d" %(f,sA,sB)) # 處理僅出現(xiàn)在一個(gè)目錄中的文件 onlyFiles = setA ^ setB aonlyFiles = [] bonlyFiles = [] for of in onlyFiles: if of in afiles: aonlyFiles.append(of) elif of in bfiles: bonlyFiles.append(of) #print ("###################### EE resource ONLY ###########################") #print ("#only files in ", apath) for of in sorted(aonlyFiles): with open (os.getcwd()+'\\EEonly.txt','a') as ee: ee.write(of+'\n') #print (of) #print ("*"*20+"SVN ONLY+"+"*"*20) #print ("#only files in ", bpath) for of in sorted(bonlyFiles): with open (os.getcwd()+'\\svnonly.txt','a') as svn: 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
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
一篇文章搞懂Python程序流程控制結(jié)構(gòu)
這篇文章主要給大家介紹了關(guān)于Python程序流程控制結(jié)構(gòu)的相關(guān)資料,本節(jié)學(xué)習(xí)了Python程序的控制結(jié)構(gòu)之順序結(jié)構(gòu)、分支結(jié)構(gòu)、循環(huán)結(jié)構(gòu),文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09Python實(shí)現(xiàn)簡單的文本相似度分析操作詳解
這篇文章主要介紹了Python實(shí)現(xiàn)簡單的文本相似度分析操作,結(jié)合實(shí)例形式分析了Python基于分詞API庫jieba及文本相似度庫gensim針對(duì)文本進(jìn)行相似度分析操作的實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2018-06-06Python TCP接收數(shù)據(jù)不全的問題解決
本文主要介紹了Python TCP接收數(shù)據(jù)不全的問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07pytorch .detach() .detach_() 和 .data用于切斷反向傳播的實(shí)現(xiàn)
這篇文章主要介紹了pytorch .detach() .detach_() 和 .data用于切斷反向傳播的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Python如何利用pandas讀取csv數(shù)據(jù)并繪圖
這篇文章主要介紹了Python如何利用pandas讀取csv數(shù)據(jù)并繪圖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07