#-*-coding:utf-8-*-
#===============================================================================
# Directory Comparison tool (contains subdirectories), and lists
# 1, a more than B what files
# 2, b more than a what files
# 3, the same files: File size is the same VS file size is different (size same file does not print: Different file display is not sorted with size)
# 4, can be modified to compare file names and include file formats or only compare filenames but do not include the format suffix.
#===============================================================================
Import OS, Time,difflib,sys
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): #获取文件大小的函数, unused, for learning only. So comment out
# size=0
# for Root, dirs, files in Os.walk (dir):
# #root: directory: str such as: C:\CopySVN\SystemObject\TopoProcedure\Built-in\
# #dirs: Directory name: list: such as [' parsers ']
# #files: Name: list: 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):
Print Apath, ' All Files numbers: ', Len (Files)
For f in Files:
#比较文件名不含格式后缀
#afiles. Append (root + f[0:-4])
#比较文件名含格式后缀
Afiles.append (root + F)
For root, dirs, files in Os.walk (Bpath):
Print Bpath, ' All Files numbers: ', Len (Files)
For f in Files:
#比较文件名不含格式后缀
#bfiles. Append (root + f[0:-4])
#比较文件名含格式后缀
Bfiles.append (root + F)
#sizeB = os.path.getsize (root + "/" + f) the size defined here cannot be compared in commonfiles. (A, B in the respective loops)
# Remove the Afiles file name in the Apath (take the same path \ filename, make a set, to find the intersection)
Apathlen = Len (apath)
Aafiles = []
For F in Afiles:
Aafiles.append (F[apathlen:])
# Remove the Bpath from the Bfiles file name
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 Common Files
#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 + "/" + 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)))
#以下代码是处理大小一致, but the content may be inconsistent
#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))
# process files that appear in only one directory
Onlyfiles = SetA ^ Setb
Aonlyfiles = []
Bonlyfiles = []
For the in Onlyfiles:
If of in Afiles:
Aonlyfiles.append (OF)
Elif of In Bfiles:
Bonlyfiles.append (OF)
Print Apath, ' Only files numbers: ', Len (aonlyfiles)
Print Bpath, ' Only files numbers: ', Len (bonlyfiles)
#print ("###################### EE resource Only ###########################")
#print ("#only files in", Apath)
If Os.path.exists (OS.GETCWD () + '/aonly.txt '):
Os.remove (OS.GETCWD () + '/aonly.txt ')
If Os.path.exists (OS.GETCWD () + '/bonly.txt '):
Os.remove (OS.GETCWD () + '/bonly.txt ')
For the in Sorted (aonlyfiles):
With open (OS.GETCWD () + '/aonly.txt ', ' a ') as a:
A.write (of+ ' \ n ')
#print (OF)
#print ("*" *20+ "SVN only+" + "*" *20)
#print ("#only files in", Bpath)
For the in Sorted (bonlyfiles):
With open (OS.GETCWD () + '/bonly.txt ', ' a ') as B:
B.write (of+ ' \ n ')
#print (OF)
if __name__ = = ' __main__ ':
Folderee = sys.argv[1]
FOLDERSVN = sys.argv[2]
Dircompare (Folderee, FOLDERSVN)
Print ("done!")
PS: This article refers to Http://www.cnblogs.com/luo-mao/p/5872532.html cat father, modified in line with their own use, thanks to the author.
Python compares the contents of two folders with generality.