Python判斷兩個文件是否相同與兩個文本進(jìn)行相同項篩選的方法
更新時間:2019年03月01日 09:37:38 作者:qq_36155051
今天小編就為大家分享一篇關(guān)于Python判斷兩個文件是否相同與兩個文本進(jìn)行相同項篩選的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
python判斷兩個文件是否相同
import hashlib def getHash(f): line=f.readline() hash=hashlib.md5() while(line): hash.update(line) line=f.readline() return hash.hexdigest() def IsHashEqual(f1,f2): str1=getHash(f1) str2=getHash(f2) return str1==str2 if __name__ == '__main__': f1=open("D:/2.iso","rb") f2=open("E:/wenjian/1.iso","rb") print IsHashEqual(f1,f2)
計算2個文件的MD5值,大文件計算較慢
python對兩個文本進(jìn)行相同項篩選
import os import os.path as osp def filter(path): file_path = osp.join(path, 'index.txt') if osp.exists(file_path): return file_path index_file = open(file_path, 'a+') if not os.path.isdir(path): #判斷path是否為路徑 return for root, dirs, list in os.walk(path): for i in list: dir = os.path.join(root, i) #將分離的部分組成一個路徑名 #if os.path.getsize(dir) < 60000: #獲取文件大小 #os.remove(dir) #刪除文件 print (i) index_file.write(i+'\n') index_file.close() def compare(path): file=osp.join(path, 'label.txt') file_path = osp.join(path, 'index.txt') with open(file_path, 'r') as file1: with open(file, 'r') as file2: same = set(file1).intersection(file2) same.discard('\n') with open('some_output_file.txt', 'w') as file_out: for line in same: file_out.write(line) file_out.close() filter(r'D:\Desktop\jiaoben\ci') compare(r'D:\Desktop\jiaoben\ci')
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
python并發(fā)2之使用asyncio處理并發(fā)
本篇文章主要介紹了python并發(fā)2之使用asyncio處理并發(fā),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12Python數(shù)據(jù)處理之savetxt()和loadtxt()使用詳解
這篇文章主要介紹了Python數(shù)據(jù)處理之savetxt()和loadtxt()使用詳解,NumPy提供了多種存取數(shù)組內(nèi)容的文件操作函數(shù),保存數(shù)組數(shù)據(jù)的文件可以是二進(jìn)制格式或者文本格式,今天我們來看看savetxt()和loadtxt()的用法,需要的朋友可以參考下2023-08-08