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


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()
posted @ 2017-07-14 23:13  Dus  阅读(404)  评论(0编辑  收藏  举报