本文實例講述了Python編程實現兩個文件夾里文件的對比功能。分享給大家供大家參考,具體如下:
#-*-coding:utf-8-*-#===============================================================================# 目錄對比工具(包含子目錄 ),并列出# 1、A比B多了哪些文件# 2、B比A多了哪些文件# 3、二者相同的文件:文件大小相同 VS 文件大小不同 (Size相同文件不打印:與Size不同文件顯示未排序)#===============================================================================import os, time,difflibAFILES = [] #EEBFILES = [] #SVNCOMMON = [] #EE & SVNdef getPrettyTime(state):  return time.strftime('%y-%m-%d %H:%M:%S', time.localtime(state.st_mtime))# def getpathsize(dir): #獲取文件大小的函數,未用上,僅供學習.故注釋掉#   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 sizedef dirCompare(apath,bpath):  afiles = []  bfiles = []  for root, dirs , files in os.walk(apath):    for f in files:      afiles.append(root + "http://" + f)  for root, dirs , files in os.walk(bpath):    for f in files:      bfiles.append(root + "http://" + f)      #sizeB = os.path.getsize(root + "http://" + f) 此處定義的size無法在commonfiles進行比較. (A,B在各自的循環里面)  # 去掉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, "'===============")  #將結果輸出到本地  #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 + "http://" + f)    sB=os.path.getsize(bpath + "http://" + f)    if sA==sB: #共有文件的大小比較      #pass #print (f + "/t/t" + getPrettyTime(os.stat(apath + "http://" + f)) + "/t/t" + getPrettyTime(os.stat(bpath + "http://" + f)))      #以下代碼是處理大小一致,但是內容可能不一致的情況      #print("in sa=sb")      #print(os.getcwd())      saf=[]      sbf=[]      sAfile=open(apath + "http://" + f)      iter_f=iter(sAfile)      for line in iter_f:        saf.append(line)      sAfile.close()      sBfile=open(bpath + "http://" + 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 + "http://" + f+" lines size not equal "+bpath + "http://" + 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 + "http://" + f+" content not equal "+bpath + "http://" + 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))  # 處理僅出現在一個目錄中的文件  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!")            
新聞熱點
疑難解答