思路:
- 源文件或文件夾夾與目標(biāo)不同名的文件有那些区赵?
- 源文件或文件夾夾與目標(biāo)同名的情況:
a)文件同名惭缰,比較文件內(nèi)容是否相同?
b)文件夾同名笼才,比較共有文件的內(nèi)容
c)文件同名漱受,但是內(nèi)容不同
程序1:找出兩個(gè)文件目錄中不同的部分
'''
比較兩個(gè)目錄里文件是否一致?
1.要列出2個(gè)文件夾中所有的文件骡送;
2.相互做比較昂羡;
3.把比較的結(jié)果以報(bào)告的形式呈現(xiàn)
'''
import os,sys
def reportdiff(unique1,unique2,dir1,dir2):
'''
生成目錄差異化報(bào)告
'''
if not (unique1 or unique2):
print("Directory lists are identical")
else:
if unique1:
print('Files unique to:',dir1)
for file in unique1:
print('....',file)
if unique2:
print('Files unique to:',dir2)
for file in unique2:
print('.........',file)
def difference(seq1,seq2):
'''
僅返回seq1中的所有項(xiàng)
'''
return [item for item in seq1 if item not in seq2]
def comparedirs(dir1,dir2,files1=None,files2=None):
'''
比較文件的名字
'''
print('Comparing...',dir1,'to....',dir2)
files1 = os.listdir(dir1) if files1 is None else files1
files2 = os.listdir(dir2) if files2 is None else files2
unique1 = difference(files1,files2)
unique2 = difference(files2,files1)
reportdiff(unique1,unique2,dir1,dir2)
return not(unique1,unique2)
def getarg():
'''
獲取參數(shù)
'''
try:
dir1,dir2 = sys.argv[1:]
except:
print("Usage: dirdiff.py dir1 dir2")
sys.exit(1)
else:
return (dir1,dir2)
if __name__=='__main__':
dir1,dir2 = getarg()
comparedirs(dir1,dir2)
程序2: 增加文件內(nèi)容的比較
import os,dirdiff
blocksize = 1024*1024
def intersect(seq1,seq2):
'''
返回seq1和seq2中的所有共有項(xiàng)絮记;
'''
return [item for item in seq1 if item in seq2]
def comparetrees(dir1,dir2,diffs,verbose=False):
'''
比較兩個(gè)目錄中的所有子目錄和文件;使用二進(jìn)制文件來阻止Unicode解碼和換行符轉(zhuǎn)換虐先;
因?yàn)槟夸洏淇赡芎卸M(jìn)制文件和文本文件怨愤;
可能需要listdir的bytes參數(shù)來處理某些平臺行不可解碼的文件名
'''
print("--"*20)
names1 = os.listdir(dir1)
names2 = os.listdir(dir2)
if not dirdiff.comparedirs(dir1,dir2,names1,names2):
diffs.append('unique file at %s - %s ' %(dir1,dir2))
print('Comparing contents')
common = intersect(names1,names2)
missed = common[:]
#比較共有文件內(nèi)容
for name in common:
path1 = os.path.join(dir1,name)
path2 = os.path.join(dir2,name)
if os.path.isfile(path1) and os.path.isfile(path2):
missed.remove(name)
file1 = open(path1,'rb')
file2 = open(path2,'rb')
while True:
byetes1 =file1.read(blocksize)
byetes2 = file2.read(blocksize)
if (not byetes1) and (not byetes2):
if verbose:print(name,'matches')
break
if byetes1 != byetes2:
diffs.append('files differ at %s --- %s ' %(path1,path2))
print(name,'DIFFERS')
break
#遞歸以比較共有目錄
for name in common:
path1 = os.path.join(dir1, name)
path2 = os.path.join(dir2, name)
if os.path.isdir(path1) and os.path.isdir(path2):
missed.remove(name)
comparetrees(path1,path2,diffs,verbose)
#同名但一個(gè)是文件,一個(gè)是目錄
for name in missed:
diffs.append('files missed at %s ---%s:%s' %(dir1,dir2,name))
print(name,'DIFFERS')
if __name__ =='__main__':
dir1,dir2 = dirdiff.getarg()
diffs = []
comparetrees(dir1,dir2,diffs,True)
print('='*40)
if not diffs:
print('No diffs found.')
else:
print('Diffs found: ',len(diffs))
for diff in diffs:
print('-',diff)