Python中使用md5sum检查目录中相同文件代码分享
admin
2023-07-31 02:09:53
0

复制代码 代码如下:
\”\”\”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)

相关内容

热门资讯

500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
python查找阿姆斯特朗数 题目解释 如果一个n位正整数等于其各位数字的n次方之和,则称该数为阿姆斯特朗数。 例如1^3 + 5...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...