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)
猜你喜欢
- 绿色在黄色和蓝色(冷暖)之间,属于较中庸的颜色,这样使得绿色的性格最为平和、安稳、大度、宽容。是一种柔顺、恬静、满足、优美、受欢迎之色。也是
- 有时候我们用的一些pdf资料是没有目录的,这样找寻我们想到的东西比较麻烦。本篇文章就为大家带来python来生成pdf目录书签的方法。首先,
- 现在正在搞三层开发,用ASP和VB6.0,但是现在苦于没有找到合适的方法来调试自己写的DLL文件,效率相当低。 &n
- 导航标签彼此互斥、完全穷尽。导航标签其实就是一种文字表达形式,我们用标签来代表网站上的各种分类信息。比如“联系我们”这个标签,代表的内容通常
- 题目描述724. 寻找数组的中心下标 - 力扣(LeetCode) (leetcode-cn.com)给你一个整数数组 nums
- 本文实例讲述了Python实现提取XML内容并保存到Excel中的方法。分享给大家供大家参考,具体如下:最近做一个项目是解析XML文件,提取
- 给密码加密是什么:用户注册的密码一般网站管理人员会利用md5方法加密,这种加密方法的好处是它是单向加密的,也就是说,你只有在提前知道某一串密
- 几天前我在考虑使用 python 从 whatsapp 发送消息。和你们一样,我开始潜伏在互联网上寻找一些解决方案并找到了关于twilio.
- HTTP暴力破解、撞库,有一些惯用的技巧,比如:1. 在扫号人人网时,我遇到单个账号错误两次,强制要求输入验证码,而对方并未实施IP策略。我
- 一般情况下,在函数中可以使用一个装饰器,但是有时也会有两个或两个以上的装饰器。多个装饰器装饰的顺序是从里到外(就近原则),而调用的顺序是从外
- iframe标签在网页中可以创建一个内嵌框架,通过指定src属性来调用另一个网页文档的内容。和frameset一样,用它来对网页结构进行拆分
- jQuery的选择器可谓异常强大,没有什么DOM里的任何数据能逃出它的掌心,这点是我非常喜欢的,以前获取NODE要用getElementBy
- 什么是 NFT?NFT英文全称为Non-Fungible Token,翻译成中文就是:非同质化代币,具有不可分割、不可替代、独一无二等特点。
- 一. 认证系统概要create_user 创建用户authenticate 验证登录login 记住用户的登录状态logout 退出登录is
- 格式为: SCRENC [/s] [/f] [/xl] [/l defLanguage ] [/e de
- 一、需求说明在Python程序的开发过程中,一些程序需要获取函数或程序的开始时间、结束时间和时间间隔等内容用来分析和处理内容二、需求分析涉及
- 我们生活在信息繁杂的社会,尤其是在互联网时代,人们开始通过网络开始接触越来越多的信息,那么,如何获取/传递有效而准确的信息将非常重要。在网页
- 环境 python -m venv venvvenv\Scripts\activate 激活环境失败 解决方案输入get-execution
- 有朋友使用DIV居中,但是却发现DIV居中失灵了,是怎么回事呢?下面给大家解决一下问题,呵呵!1、一般情况下DIV居中失效是因为没写DTD语
- 代码之余,将代码过程重要的一些代码段备份一下,如下的代码内容是关于Python从ftp服务器下载文件的的代码,希望能对小伙伴有用途。#cod