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

python實(shí)現(xiàn)對(duì)文件進(jìn)行MD5校驗(yàn)

 更新時(shí)間:2024年01月26日 08:55:08   作者:XXYBMOOO  
這篇文章主要為大家詳細(xì)介紹了如何使用python對(duì)文件進(jìn)行MD5校驗(yàn)并比對(duì)文件重復(fù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

MD5校驗(yàn)(checksum)是通過對(duì)接收的傳輸數(shù)據(jù)執(zhí)行散列運(yùn)算來檢查數(shù)據(jù)的正確性。MD5校驗(yàn)可以應(yīng)用多個(gè)領(lǐng)域,比如說機(jī)密資料的檢驗(yàn),下載文件的檢驗(yàn),明文密碼的加密等。

本文主要為大家介紹了如何使用python對(duì)文件進(jìn)行MD5校驗(yàn)并比對(duì)文件重復(fù)

下面是實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助

import os
import time
import hashlib
import re
from concurrent.futures import ProcessPoolExecutor
from functools import partial
 
def generate_md5_for_file(file_path, block_size=4096):
    # Calculate the MD5 hash for a given file
    md5_hash = hashlib.md5()
    with open(file_path, "rb") as f:
        for byte_block in iter(partial(f.read, block_size), b""):
            md5_hash.update(byte_block)
    return file_path, md5_hash.hexdigest()
 
def generate_md5_for_files_parallel(folder_path, block_size=4096):
    # Generate MD5 hashes for all files in a folder using parallel processing
    md5_dict = {}
    with ProcessPoolExecutor() as executor:
        # Get all file paths in the specified folder
        file_paths = [os.path.join(root, file) for root, _, files in os.walk(folder_path) for file in files]
        # Use parallel processing to calculate MD5 hashes for each file
        results = executor.map(partial(generate_md5_for_file, block_size=block_size), file_paths)
 
    # Update the dictionary with the calculated MD5 values
    md5_dict.update(results)
    return md5_dict
 
def write_md5_to_file(md5_dict, output_file):
    # Write MD5 values and file paths to a text file
    with open(output_file, "w") as f:
        for file_path, md5_value in md5_dict.items():
            f.write(f"{md5_value}  {file_path}\n")
 
def check_duplicate_md5(file_path):
    # Check for duplicate MD5 values in a text file
    md5_dict = {}
    with open(file_path, "r") as f:
        for line in f:
            line = line.strip()
            if line:
                md5_value, file_path = line.split(" ", 1)
                if md5_value in md5_dict:
                    # Print information about duplicate MD5 values
                    print(f"Duplicate MD5 found: {md5_value}")
                    print(f"Original file: {md5_dict[md5_value]}")
                    print(f"Duplicate file: {file_path}\n")
                else:
                    md5_dict[md5_value] = file_path
 
def split_and_check_duplicate_part(filename, part_index, seen_parts):
    # Split a filename using "_" and check for duplicate parts
    parts = filename.split("_")
    if len(parts) == 4:
        selected_part = parts[part_index]
        if selected_part in seen_parts:
            # Print information about duplicate parts
            print(f'Duplicate part found at index {part_index}: {selected_part}')
        else:
            seen_parts.add(selected_part)
    else:
        # Print information if the filename does not have four parts
        print(f'File "{filename}" does not have four parts.')
 
def process_folder(folder_path, part_index):
    # Process all filenames in a folder
    files = os.listdir(folder_path)
    seen_parts = set()
    for filename in files:
        # Call the split_and_check_duplicate_part function
        split_and_check_duplicate_part(filename, part_index, seen_parts)
 
def find_max_execution_time(file_path):
    # Find the maximum execution time from a log file
    try:
        with open(file_path, 'r') as file:
            numbers = []
            pattern = re.compile(r'Program execution time: (\d+) microseconds')
            for line in file:
                match = pattern.search(line)
                if match:
                    numbers.append(int(match.group(1)))
            if not numbers:
                raise ValueError("No execution time found in the file.")
            max_number = max(numbers)
            return max_number
    except FileNotFoundError:
        raise FileNotFoundError(f"Error: File '{file_path}' not found.")
    except Exception as e:
        raise Exception(f"An error occurred: {e}")
 
if __name__ == "__main__":
    # Record the start time of the program
    start_time = time.time()
 
    # Set the folder path and log file path
    folder_path = r"D:/outputFile/bmp"
    file_path = r"D:/log.txt"
 
    try:
        # Try to find and print the maximum execution time
        max_execution_time = find_max_execution_time(file_path)
        print(f"The maximum execution time is: {max_execution_time} microseconds")
    except Exception as e:
        # Print an error message if an exception occurs
        print(e)
 
    # Set the index of the part to be compared
    selected_part_index = 1
 
    # Call the process_folder function to handle filenames
    process_folder(folder_path, selected_part_index)
 
    # Set the MD5 file path and block size
    MD5_file = "D:/md5sums.txt"
    block_size = 8192
 
    # Generate MD5 values for files in parallel and write them to a file
    md5_dict = generate_md5_for_files_parallel(folder_path, block_size=block_size)
    write_md5_to_file(md5_dict, MD5_file)
 
    # Print a message indicating successful MD5 generation
    print(f"MD5 values generated and saved to {MD5_file}")
 
    # Check for duplicate MD5 values in the generated file
    check_duplicate_md5(MD5_file)
 
    # Record the end time of the program
    end_time = time.time()
 
    # Calculate the total execution time in milliseconds
    execution_time = (end_time - start_time) * 1000
    print(f"Function execution time: {execution_time} milliseconds")

到此這篇關(guān)于python實(shí)現(xiàn)對(duì)文件進(jìn)行MD5校驗(yàn)的文章就介紹到這了,更多相關(guān)python文件MD5校驗(yàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Django使用mysqlclient服務(wù)連接并寫入數(shù)據(jù)庫(kù)的操作過程

    Django使用mysqlclient服務(wù)連接并寫入數(shù)據(jù)庫(kù)的操作過程

    這篇文章主要介紹了Django使用mysqlclient服務(wù)連接并寫入數(shù)據(jù)庫(kù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • Python中元組的概念及應(yīng)用小結(jié)

    Python中元組的概念及應(yīng)用小結(jié)

    Python中的元組和列表很相似,元組也是Python語言提供的內(nèi)置數(shù)據(jù)結(jié)構(gòu)之一,可以在代碼中直接使用,這篇文章主要介紹了Python中元組的概念以及應(yīng)用,需要的朋友可以參考下
    2023-01-01
  • Python數(shù)據(jù)擬合與廣義線性回歸算法學(xué)習(xí)

    Python數(shù)據(jù)擬合與廣義線性回歸算法學(xué)習(xí)

    這篇文章主要為大家詳細(xì)介紹了Python數(shù)據(jù)擬合與廣義線性回歸算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Python使用MD5加密算法對(duì)字符串進(jìn)行加密操作示例

    Python使用MD5加密算法對(duì)字符串進(jìn)行加密操作示例

    這篇文章主要介紹了Python使用MD5加密算法對(duì)字符串進(jìn)行加密操作,結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)md5加密相關(guān)操作技巧,需要的朋友可以參考下
    2018-03-03
  • 對(duì)Python中g(shù)ensim庫(kù)word2vec的使用詳解

    對(duì)Python中g(shù)ensim庫(kù)word2vec的使用詳解

    今天小編就為大家分享一篇對(duì)Python中g(shù)ensim庫(kù)word2vec的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • 詳解pandas繪制矩陣散點(diǎn)圖(scatter_matrix)的方法

    詳解pandas繪制矩陣散點(diǎn)圖(scatter_matrix)的方法

    這篇文章主要介紹了詳解pandas繪制矩陣散點(diǎn)圖(scatter_matrix)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Python類中方法getitem和getattr詳解

    Python類中方法getitem和getattr詳解

    這篇文章主要介紹了Python類中方法getitem和getattr詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • python入門學(xué)習(xí)筆記分享

    python入門學(xué)習(xí)筆記分享

    這篇文章主要介紹了關(guān)于Python的一些總結(jié),希望自己以后在學(xué)習(xí)Python的過程中可以邊學(xué)習(xí)邊總結(jié),就自己之前的學(xué)習(xí)先做以總結(jié),之后將不斷總結(jié)更新
    2021-10-10
  • python中的set實(shí)現(xiàn)不重復(fù)的排序原理

    python中的set實(shí)現(xiàn)不重復(fù)的排序原理

    這篇文章主要介紹了python中的set實(shí)現(xiàn)不重復(fù)的排序原理,需要的朋友可以參考下
    2018-01-01
  • Python中類方法@classmethod和靜態(tài)方法@staticmethod解析

    Python中類方法@classmethod和靜態(tài)方法@staticmethod解析

    這篇文章主要介紹了Python中類方法@classmethod和靜態(tài)方法@staticmethod解析,python中存在三種方法,分別為常規(guī)方法(定義中傳入self)、@classmethod修飾的類方法、@staticmethod修飾的靜態(tài)方法,,需要的朋友可以參考下
    2023-08-08

最新評(píng)論

乡城县| 长宁区| 定兴县| 泗阳县| 沁阳市| 井研县| 延长县| 南汇区| 乳山市| 常宁市| 深圳市| 沁阳市| 上犹县| 婺源县| 吉安县| 徐汇区| 迁安市| 勐海县| 阳江市| 栖霞市| 闸北区| 宁陕县| 灌阳县| 延寿县| 丘北县| 佛山市| 固阳县| 宾川县| 罗平县| 邵武市| 武平县| 陇西县| 长武县| 兴化市| 尼玛县| 黑山县| 日土县| 太仓市| 神木县| 大安市| 枝江市|