您可以使用Python的hashlib庫來計(jì)算文件的MD5值,并使用json庫將結(jié)果輸出到文件。以下是一個(gè)實(shí)現(xiàn)該功能的代碼示例:
import hashlib import json import os def calculate_md5(file_path): md5_hash = hashlib.md5() with open(file_path, "rb") as file: for chunk in iter(lambda: file.read(4096), b""): md5_hash.update(chunk) return md5_hash.hexdigest() def generate_md5_json(directory_path, output_file_path): md5_dict = {} for root, dirs, files in os.walk(directory_path): for filename in files: file_path = os.path.join(root, filename) md5 = calculate_md5(file_path) md5_dict[file_path] = md5 with open(output_file_path, "w") as output_file: json.dump(md5_dict, output_file, indent=4) # 指定目錄路徑和輸出文件路徑 directory_path = "/path/to/directory" output_file_path = "/path/to/output.json" generate_md5_json(directory_path, output_file_path)
請將`/path/to/directory`替換為您想要計(jì)算MD5值的目錄的實(shí)際路徑,將`/path/to/output.json`替換為保存輸出JSON文件的路徑。運(yùn)行代碼后,指定目錄下的所有文件的MD5值將會以JSON格式輸出到指定的文件中。文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.strong-digital.cn/11060.html
請注意,上述代碼遞歸地遍歷指定目錄及其子目錄中的所有文件,并計(jì)算每個(gè)文件的MD5值。這可能需要一些時(shí)間,具體取決于目錄中文件的數(shù)量和大小。文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.strong-digital.cn/11060.html 文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.strong-digital.cn/11060.html
版權(quán)聲明:文章圖片資源來源于網(wǎng)絡(luò),如有侵權(quán),請留言刪除!!!
評論