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

 更新时间:2015年02月02日 09:22:01   投稿:junjie  
这篇文章主要介绍了Python中使用md5sum检查目录中相同文件代码分享,本文直接给出实现代码,需要的朋友可以参考下

复制代码 代码如下:

"""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使用pptx实现复制页面到其他PPT中

    Python使用pptx实现复制页面到其他PPT中

    这篇文章主要为大家详细介绍了python如何使用pptx库实现从一个ppt复制页面到另一个ppt里面,文中的示例代码讲解详细,感兴趣的可以尝试一下
    2023-02-02
  • CentOS中安装python3.8.2的详细教程

    CentOS中安装python3.8.2的详细教程

    这篇文章主要介绍了CentOS中安装python3.8.2的详细教程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • Python cookbook(数据结构与算法)筛选及提取序列中元素的方法

    Python cookbook(数据结构与算法)筛选及提取序列中元素的方法

    这篇文章主要介绍了Python cookbook(数据结构与算法)筛选及提取序列中元素的方法,涉及Python列表推导式、生成器表达式及filter()函数相关使用技巧,需要的朋友可以参考下
    2018-03-03
  • Tensorflow深度学习使用CNN分类英文文本

    Tensorflow深度学习使用CNN分类英文文本

    这篇文章主要为大家介绍了Tensorflow深度学习CNN实现英文文本分类示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2021-11-11
  • 使用Python和Scribus创建一个RGB立方体的方法

    使用Python和Scribus创建一个RGB立方体的方法

    这篇文章主要介绍了使用Python和Scribus创建一个RGB立方体的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • 详解opencv rtsp 硬件解码

    详解opencv rtsp 硬件解码

    这篇文章主要介绍了opencv rtsp硬件解码的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-08-08
  • Python+Flask编写一个简单的行人检测API

    Python+Flask编写一个简单的行人检测API

    Flask是一个微型的Python开发的Web框架,基于Werkzeug WSGI工具箱和Jinja2模板引擎。本文将利用Flask框子编写一个简单的行人检测API,感兴趣的可以了解一下
    2022-03-03
  • 详解Python中的三器一闭

    详解Python中的三器一闭

    这篇文章主要介绍了详解Python中的三器一闭,Python中的三器一闭是指迭代器、装饰器、生成器和闭包,需要的朋友可以参考下
    2023-05-05
  • Python virtualenv虚拟环境实现过程解析

    Python virtualenv虚拟环境实现过程解析

    这篇文章主要介绍了Python virtualenv虚拟环境实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • 讲解Python中fileno()方法的使用

    讲解Python中fileno()方法的使用

    这篇文章主要介绍了讲解Python中fileno()方法的使用,是Python入门中的基础知识,需要的朋友可以参考下
    2015-05-05

最新评论