gpt4 book ai didi

Python判断两个文件是否相同与两个文本进行相同项筛选的方法

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Python判断两个文件是否相同与两个文本进行相同项筛选的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

python判断两个文件是否相同 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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对两个文本进行相同项筛选 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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' )

总结 。

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我的支持。如果你想了解更多相关内容请查看下面相关链接 。

原文链接:https://blog.csdn.net/qq_36155051/article/details/76215081 。

最后此篇关于Python判断两个文件是否相同与两个文本进行相同项筛选的方法的文章就讲到这里了,如果你想了解更多关于Python判断两个文件是否相同与两个文本进行相同项筛选的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com