您的位置:首页 > 其它

从命令行获取两个路径名称并找出文件一样内容一样的两个文件

2017-07-14 23:13 281 查看
import os
import sys
import subprocess
import hashlib

dir1 = sys.argv[1]
dir2 = sys.argv[2]

class FileCompare(object):
"""从命令行获取两个路径名称并找出文件一样内容一样的两个文件"""
re_code, re = subprocess.getstatusoutput("find %s -type f" % dir1)
re_code_1, re_1 = subprocess.getstatusoutput("find %s -type f" % dir2)

def __init__(self):
pass

def md5_file(self, f1 ,f2):
"""文件内容校验"""
with open(f1, 'r') as file1, open(f2, 'r') as file2:
file1_md5 = hashlib.md5(file1.read())
file2_md5 = hashlib.md5(file2.read())
if file1_md5 == file2_md5:
return True
return False

def file_compare(self):
"""进行名称和MD5对比"""
for i in self.re:
for j in self.re_1:
if j.split() == i.split() and self.md5_file(i, j):
print("文件名:",i.split(),"------","路径为:",i)
break

FileCompare().file_compare()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐