python判断相同,Python判断两个文件是否相同与两个文本进行相同项筛选的方法
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對兩個文本進行相同項篩選
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')
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對萬仟網的支持。如果你想了解更多相關內容請查看下面相關鏈接
如您對本文有疑問或者有任何想說的,請點擊進行留言回復,萬千網友為您解惑!
總結
以上是生活随笔為你收集整理的python判断相同,Python判断两个文件是否相同与两个文本进行相同项筛选的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 玫瑰花函数matlab,网上收到的用ma
- 下一篇: 儿童学python下哪个软件好用_【开源