是否有很多抓到過的
這邊用os跟filecmp這兩個module來實現一個自動找尋有那些是完全相同的檔案
最後你再自己決定那些檔案要砍掉
#!/usr/bin/env python
import os,sys
import filecmp
def GetAllFiles(path, all_files):
print 'Start read file...'
file_num = 0
#找尋路徑中所有檔案
for root,dirs, files in os.walk(path):
for f in files:
new_path = os.path.join(root,f)
try:
#因為找尋所有檔案花的時間太長而且解救不了太多的檔案空間
#所以這邊我只找尋比100MB大的檔案
#100*1024*1024 = 104857600
if os.path.getsize(new_path) >= 104857600:
file_num += 1
sys.stdout.write(chr(0x0d))
sys.stdout.write(str(file_num))
sys.stdout.flush()
#存下所有比100MB大的檔案路徑
all_files.append(new_path)
except:
pass
def CompareFiles(all_files):
total_file = str(len(all_files))
file_num = 0
for f1 in all_files:
file_num += 1
sys.stdout.write(chr(0x0d))
sys.stdout.write(str(file_num)+'/'+total_file)
sys.stdout.flush()
for f2 in all_files:
#如果說f1跟f2不是同一個地方的檔案
if f1[:] != f2[:]:
if os.path.exists(f1) == True and os.path.exists(f2) == True:
#檢查是不是一樣的檔案, 如果是的話就印出兩個檔案的位置
if filecmp.cmp(f1,f2) == True:
print f1,'\t',f2
if __name__ == '__main__':
all_files = []
GetAllFiles('/', all_files)
CompareFiles(all_files)
沒有留言:
張貼留言