Python programming implementation of two folders in the file comparison function example [containing content comparison]

  • 2020-06-07 04:41:37
  • OfStack

This article is an example of Python programming to compare files in two folders. To share for your reference, specific as follows:


#-*-coding:utf-8-*-
#===============================================================================
#  Directory comparison tool ( Include subdirectories  ) And lists 
# 1 , A than B What files are there 
# 2 , B than A What files are there 
# 3 , 2 The same file : Same file size  VS  Different file sizes  (Size The same file is not printed : with Size Different files are not displayed sorted )
#===============================================================================
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): # Function to get the file size , Not to use , Just for learning . Therefore, comment out the 
#   size=0
#   for root, dirs, files in os.walk(dir):
#   #root: directory :str  Such as : C:\CopySVN\SystemObject\TopoProcedure\Built-in\
#   #dirs: Directory name : The list of :  Such as  ['Parsers']
#   #files: The name of the : The list of :  Such as  ['011D0961FB42416AA49D5E82945DE7E9.og',...]
#   #file: directory :str,  Such as  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)  Defined here size Can't in commonfiles To compare the . (A,B In their respective cycles )
  #  To get rid of afiles Medium filename apath ( take A,B Same path \ The file name , Make a collection , To find the intersection )
  apathlen = len(apath)
  aafiles = []
  for f in afiles:
    aafiles.append(f[apathlen:])
  #  To get rid of bfiles Medium filename 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 #  Handling of common files 
  #print ("===============File with different size in '", apath, "' and '", bpath, "'===============")
  # Output the results locally 
  #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: # The size comparison of the common files 
      #pass #print (f + "\t\t" + getPrettyTime(os.stat(apath + "\\" + f)) + "\t\t" + getPrettyTime(os.stat(bpath + "\\" + f)))
      # The following code is the processing size 1 Yes, but the content may not 1 To the situation 
      #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))
  #  Processing only occurs in 1 Files in a directory 
  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: Here's another online tool with similar functions:

Online text comparison tools:
http://tools.ofstack.com/aideddesign/txt_diff

More about Python related content interested readers to view this site project: "Python file and directory skills summary", "Python skills summary text file", "Python data structure and algorithm tutorial", "Python function using skills summary", "Python string skills summary" and "Python introductory and advanced tutorial"

I hope this article is helpful for Python programming.


Related articles: