最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中使用md5sum檢查目錄中相同文件代碼分享

 更新時間:2015年02月02日 09:22:01   投稿:junjie  
這篇文章主要介紹了Python中使用md5sum檢查目錄中相同文件代碼分享,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

"""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)

相關(guān)文章

  • Python使用pptx實現(xiàn)復(fù)制頁面到其他PPT中

    Python使用pptx實現(xiàn)復(fù)制頁面到其他PPT中

    這篇文章主要為大家詳細(xì)介紹了python如何使用pptx庫實現(xiàn)從一個ppt復(fù)制頁面到另一個ppt里面,文中的示例代碼講解詳細(xì),感興趣的可以嘗試一下
    2023-02-02
  • CentOS中安裝python3.8.2的詳細(xì)教程

    CentOS中安裝python3.8.2的詳細(xì)教程

    這篇文章主要介紹了CentOS中安裝python3.8.2的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)篩選及提取序列中元素的方法

    Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)篩選及提取序列中元素的方法

    這篇文章主要介紹了Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)篩選及提取序列中元素的方法,涉及Python列表推導(dǎo)式、生成器表達(dá)式及filter()函數(shù)相關(guān)使用技巧,需要的朋友可以參考下
    2018-03-03
  • Tensorflow深度學(xué)習(xí)使用CNN分類英文文本

    Tensorflow深度學(xué)習(xí)使用CNN分類英文文本

    這篇文章主要為大家介紹了Tensorflow深度學(xué)習(xí)CNN實現(xiàn)英文文本分類示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-11-11
  • 使用Python和Scribus創(chuàng)建一個RGB立方體的方法

    使用Python和Scribus創(chuàng)建一個RGB立方體的方法

    這篇文章主要介紹了使用Python和Scribus創(chuàng)建一個RGB立方體的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 詳解opencv?rtsp?硬件解碼

    詳解opencv?rtsp?硬件解碼

    這篇文章主要介紹了opencv rtsp硬件解碼的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • Python+Flask編寫一個簡單的行人檢測API

    Python+Flask編寫一個簡單的行人檢測API

    Flask是一個微型的Python開發(fā)的Web框架,基于Werkzeug WSGI工具箱和Jinja2模板引擎。本文將利用Flask框子編寫一個簡單的行人檢測API,感興趣的可以了解一下
    2022-03-03
  • 詳解Python中的三器一閉

    詳解Python中的三器一閉

    這篇文章主要介紹了詳解Python中的三器一閉,Python中的三器一閉是指迭代器、裝飾器、生成器和閉包,需要的朋友可以參考下
    2023-05-05
  • Python virtualenv虛擬環(huán)境實現(xiàn)過程解析

    Python virtualenv虛擬環(huán)境實現(xiàn)過程解析

    這篇文章主要介紹了Python virtualenv虛擬環(huán)境實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • 講解Python中fileno()方法的使用

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

    這篇文章主要介紹了講解Python中fileno()方法的使用,是Python入門中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-05-05

最新評論

斗六市| 邳州市| 百色市| 东丽区| 姜堰市| 金湖县| 祥云县| 霍山县| 夏邑县| 长治市| 浮山县| 巩义市| 出国| 同德县| 同德县| 汤阴县| 洱源县| 溧水县| 上林县| 达孜县| 泸定县| 香格里拉县| 来安县| 偃师市| 保山市| 晋中市| 乌审旗| 安泽县| 霍城县| 临海市| 博爱县| 珲春市| 柘荣县| 博客| 宾阳县| 铁力市| 宝鸡市| 九龙坡区| 巴中市| 二连浩特市| 邯郸县|