【问题标题】:Generating MD5 hashes for all files in the current folder including all files in subfolders为当前文件夹中的所有文件(包括子文件夹中的所有文件)生成 MD5 哈希
【发布时间】:2020-07-11 21:25:52
【问题描述】:

在这个帖子的帮助下

https://codereview.stackexchange.com/questions/147056/short-script-to-hash-files-in-a-directory

我几乎完全得到了我需要的东西。给定的代码是

from os import listdir, getcwd
from os.path import isfile, join, normpath, basename
import hashlib


def get_files():
    current_path = normpath(getcwd())
    return [join(current_path, f) for f in listdir(current_path) if isfile(join(current_path, f))]


def get_hashes():
    files = get_files()
    list_of_hashes = []
    for each_file in files:
        hash_md5 = hashlib.md5()
        with open(each_file, "rb") as f:
            for chunk in iter(lambda: f.read(4096), b""):
                hash_md5.update(chunk)
        list_of_hashes.append('Filename: {}\tHash: {}\n'.format(basename(each_file), hash_md5.hexdigest()))
    return list_of_hashes


def write_hashes():
    hashes = get_hashes()
    with open('list_of_hashes.txt', 'w') as f:
        for md5_hash in hashes:
            f.write(md5_hash)


if __name__ == '__main__':
    write_hashes()

但是,另外我想考虑我给定路径的子文件夹中的所有文件,并将它们包含在输出中。我尝试使用 os.walk() 但我没有成功。

你能帮我调整一下函数 get_files() 以便它为子文件夹中的所有文件生成 MD5 哈希(即考虑整个文件夹结构吗?)

感谢您的帮助!

【问题讨论】:

    标签: python directory md5 subdirectory


    【解决方案1】:

    试试这个:

    current_path = normpath(getcwd())
    listOfFiles = []
    for (dirpath, dirnames, filenames) in walk(current_path):
        listOfFiles += [join(dirpath, file) for file in filenames]
    

    (基于this source)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 2017-07-05
      • 1970-01-01
      相关资源
      最近更新 更多