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)


猜你喜欢
- 把列表传递给函数后, 函数就能直接访问列表中的内容咯。假设有一组专家,我们想邀请他们参加研讨会。def send_invitation(ex
- 0. 学习目标我们已经知道算法是具有有限步骤的过程,其最终的目的是为了解决问题,而根据我们的经验,同一个问题的解决方法通常并非唯一。这就产生
- print() 函数使用以 % 开头的转换说明符对各种类型的数据进行格式化输出。转换说明符(Conversion Specifier)只是一
- 一、基于PaddleSpeech的婴儿啼哭识别1.项目背景对婴儿来说,啼哭声是一种通讯的方式,一个非常有限的,但类似成年人进行交流的方式。它
- 读写中文需要读取utf-8编码的中文文件,先利用sublime text软件将它改成无DOM的编码,然后用以下代码:with codecs.
- 简介查看百度搜索中文文本聚类我失望的发现,网上竟然没有一个完整的关于Python实现的中文文本聚类(乃至搜索关键词python 中文文本聚类
- 前言:决策树是梯度提升机和随机森林的基本构建块,在学习这些模型的工作原理和模型可解释性时,可视化决策树是一个非常有帮助。不过,当前的可视化包
- MongoDB是由C++语言编写的非关系型数据库,是一个基于分布式文件存储的开源数据库系统,其内容存储形式类似JSON对象,它的字段值可以包
- 本文实例讲述了Python callable()函数用法。分享给大家供大家参考,具体如下:python中的内建函数callable( ) ,
- 操作:输入带分页的地址,去掉最后面的数字,设置一下起始页数和终点页数功能:下载对应页码的所有页面并储存为HTML文件,以当前时间命名代码:#
- win7 +Navicat Lite 9+ VMware7在VMware中安装openSUSE11.x mysql5 Navicat Lit
- 背景我们先来看看MySQL 8.0的事务提交的大致流程以上流程,是MySQL8.0对WAL原则的一种实现,这个流程意味着,任何一个事务的提交
- 想到一个好玩的,运行如下 javascript :if ('0') alert("'0' is t
- 但灵活应用CSS会有给人眼前一亮的感觉! 以下用一个简单的例子来阐述我想说的。 CSS代码: #nav li ul { display:no
- hypot()方法返回的欧几里德范数 sqrt(x*x + y*y).语法以下是hypot()方法的语法:hypot(x, y)
- 前言本文主要介绍的是关于python 3用BeautifulSoup抓取div标签的方法示例,分享出来供大家参考学习,下面来看看详细的介绍:
- 小编使用python中的django框架来完成!1,首先用pycharm创建django项目并配置相关环境这里小编默认项目都会创建setti
- 由于工作需要本文主结合了excel表格,对表格中的ssh密码进行批量修改以下是详细代码(python3):'''遇到
- 最近有个部署需求,需要读取py文件格式的配置项,我的实现思路是把配置文件解析到内存中。主要使用两种方法:importlib.import_m
- 前言在目标检测中,数据集常常使用labelimg标注,会生成xml文件。本文旨在根据xml标注文件来裁剪目标,以达到去除背景信息的目的。xm