python判斷兩個文件是否相同
import hashlibdef 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==str2if __name__ == '__main__':  f1=open("D:/2.iso","rb")  f2=open("E:/wenjian/1.iso","rb")  print IsHashEqual(f1,f2)計算2個文件的MD5值,大文件計算較慢
python對兩個文本進行相同項篩選
import osimport os.path as ospdef 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')總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林站長站的支持。如果你想了解更多相關內容請查看下面相關鏈接
新聞熱點
疑難解答