Python中使用md5sum检查目录中相同文件代码分享

作者:junjie 时间:2022-10-31 19:57:59 


"""This module contains code from
Think Python by Allen B. Downey

http://thinkpython.com

Copyright 2012 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html

"""

import os

def walk(dirname):
    """Finds the names of all files in dirname and its subdirectories.

    dirname: string name of directory
    """
    names = []
    for name in os.listdir(dirname):
        path = os.path.join(dirname, name)

        if os.path.isfile(path):
            names.append(path)
        else:
            names.extend(walk(path))
    return names


def compute_checksum(filename):
    """Computes the MD5 checksum of the contents of a file.

    filename: string
    """
    cmd = 'md5sum ' + filename
    return pipe(cmd)


def check_diff(name1, name2):
    """Computes the difference between the contents of two files.

    name1, name2: string filenames
    """
    cmd = 'diff %s %s' % (name1, name2)
    return pipe(cmd)


def pipe(cmd):
    """Runs a command in a subprocess.

    cmd: string Unix command

    Returns (res, stat), the output of the subprocess and the exit status.
    """
    fp = os.popen(cmd)
    res = fp.read()
    stat = fp.close()
    assert stat is None
    return res, stat


def compute_checksums(dirname, suffix):
    """Computes checksums for all files with the given suffix.

    dirname: string name of directory to search
    suffix: string suffix to match

    Returns: map from checksum to list of files with that checksum
    """
    names = walk(dirname)

    d = {}
    for name in names:
        if name.endswith(suffix):
            res, stat = compute_checksum(name)
            checksum, _ = res.split()

            if checksum in d:
                d[checksum].append(name)
            else:
                d[checksum] = [name]

    return d


def check_pairs(names):
    """Checks whether any in a list of files differs from the others.

    names: list of string filenames
    """
    for name1 in names:
        for name2 in names:
            if name1 < name2:
                res, stat = check_diff(name1, name2)
                if res:
                    return False
    return True


def print_duplicates(d):
    """Checks for duplicate files.

    Reports any files with the same checksum and checks whether they
    are, in fact, identical.

    d: map from checksum to list of files with that checksum
    """
    for key, names in d.iteritems():
        if len(names) > 1:
            print 'The following files have the same checksum:'
            for name in names:
                print name

            if check_pairs(names):
                print 'And they are identical.'


if __name__ == '__main__':
    d = compute_checksums(dirname='.', suffix='.py')
    print_duplicates(d)

标签:Python,md5sum,目录,相同文件
0
投稿

猜你喜欢

  • 详解Python中string模块除去Str还剩下什么

    2021-08-25 12:48:19
  • asp使用 sql_dmo 添加新数据库代码

    2010-03-17 20:57:00
  • 人脸检测实战终极之OpenCV+Python实现人脸对齐

    2023-10-01 02:03:07
  • Python爬虫HTPP请求方法有哪些

    2023-07-25 16:55:06
  • Mootools 1.2教程(19)——Tooltips

    2008-12-25 13:26:00
  • python实现KNN分类算法

    2023-03-01 07:53:36
  • JS target与currentTarget区别说明

    2023-08-22 20:14:40
  • 兼容主流浏览器,纯CSS下拉菜单

    2010-09-05 20:30:00
  • PHP实现web socket长链接流程详解

    2023-05-27 23:44:39
  • 解决selenium模块利用performance获取network日志请求报错的问题(亲测有效)

    2022-12-22 11:09:26
  • Python随机数模块详情

    2021-10-26 06:47:34
  • asp简单的仿图片验证码

    2008-03-12 11:54:00
  • python实现网站用户名密码自动登录功能

    2021-07-05 09:48:13
  • Python2.x与3​​.x版本有哪些区别

    2023-11-01 08:34:31
  • 浅析MySQL数据库授权原则

    2009-12-15 09:21:00
  • python加速器numba使用详解

    2022-02-27 15:24:22
  • IE6,7下实现white-space:pre-wrap;

    2009-12-31 18:30:00
  • 一篇文章带你学习python的函数与类

    2023-10-15 05:04:18
  • 在SUSE10环境下安装和配置MySQL数据库

    2008-12-17 15:03:00
  • 教你用Type Hint提高Python程序开发效率

    2023-10-21 03:42:24
  • asp之家 网络编程 m.aspxhome.com