-3
我有兩個不同的目錄,具有不同的目錄樹結構。目錄A有一些在目錄B中的文件,反之亦然,我想確保這些文件的大小相同,以便我知道哪個副本是正確的(如果它們不同,則需要更大的副本)。這些文件是〜1-2 GB。另外,這些目錄有多種文件類型,我只想比較具有特定擴展名的文件。在Python中的目錄之間遞歸地比較重複命名文件的文件大小
如何比較類似文件的文件大小並輸出匹配和不匹配的列表?
謝謝:)
更新:很抱歉的含糊不清的問題,我是新來的堆棧溢出。我更多地研究了這一點,並能夠弄清楚。解決方案如下。對於此測試,有兩個目錄test1 /和test2 /都包含file1.txt和file2.txt。 file1.txt在兩個dirs之間是相同的,file2.txt是不同的。
d1_contents = set(os.path.basename(x) for x in glob.glob("/Users/raycharles/Desktop/test1/*.txt"))
#print d1_contents
d2_contents = set(os.path.basename(x) for x in glob.glob("/Users/raycharles/Desktop/test2/*.txt"))
#print d2_contents
common = list(d1_contents & d2_contents)
common_files = [ f
for f in common
if os.path.isfile(os.path.join('/Users/raycharles/Desktop/test1/', f))]
print 'Common files:', common_files
# Compare the directories
match, mismatch, errors = filecmp.cmpfiles('/Users/raycharles/Desktop/test1/',
'/Users/raycharles/Desktop/test2/',
common_files, shallow=True)
match = sorted(match)
mismatch = sorted(mismatch)
errors = sorted(errors)
print 'Match:', match
print ""
print 'Mismatch:', mismatch
print ""
print 'Errors:', errors
print ""
這是輸出:
Common files: ['file1.txt', 'file2.txt']
Match: ['file1.txt']
Mismatch: ['file2.txt']
Errors: []
它看起來像你希望我們爲你寫一些代碼。儘管許多用戶願意爲遇險的編碼人員編寫代碼,但他們通常只在海報已嘗試自行解決問題時才提供幫助。展示這一努力的一個好方法是包含迄今爲止編寫的代碼,示例輸入(如果有),預期輸出以及實際獲得的輸出(控制檯輸出,回溯等)。您提供的細節越多,您可能會收到的答案就越多。檢查[FAQ](http://stackoverflow.com/tour)和[如何提問](http://stackoverflow.com/help/how-to-ask)。 –