在 python 中比较文件、检查文件名、文件大小和校验和 md5 的路径

我想比较具有路径、文件名、文件大小和 md5 校验和的目录中的文件。当我独立检查它们时,我得到了三个对文件名、文件大小和 md5 校验和工作正常的函数。我认为问题在于我如何设置另一个函数来处理包含 csv 文件项的字典。这是要比较的 csv 文件。


|Path|Filename|File Size|Hash

|/var/tmp/test|test1.txt|257|2e6041635f72233f4cdf6fbfb0a8288e

|/var/tmp/test|text2.txt|68|d3428d5910f54270d62ff57ccd5ff52c

|/var/tmp/test|text3.txt|58|42e8b3cba5320e07745110b8b193f534

|/var/tmp/test|text4.xml|128|4acc96e6e8b9006722408e15e555d2c2

|/var/tmp/test|text5.csv|214|a7071c13195d8485b2fb4a68503cbd7a


我试图修改 md5、文件名、文件大小以及它如何在目录中循环,但似乎有问题。


def csv_checksum(files, path):

    # Get column with delimiter

    csv.register_dialect('myDialect', delimiter = '|')


    csvDics = {}

    # Open file, read them, and output csv formatted

    with open(files, 'r') as f:

        reader = csv.reader(f, dialect='myDialect')

        for row in reader:

            if reader.line_num == 1:

                continue

            csvDic = {

                    'Directory': row[1],

                    'Filename': row[2],

                    'File Size': row[3],

                    'Hash': row[4]

            }

            csvDics.update(csvDic) 

            print(csvDics)

            comp_original(csvDics, path)


def comp_original(dic, path):

    for (dirpath, dirnames, filenames) in os.walk(path):

        for files in filenames:

            if (dic.get('Directory') == path

                    and dic.get('Filename') == get_filename(files)

                    and dic.get('File Size') == get_filesize(files)

                    and dic.get('Hash') == get_md5(files)):

                print("All files matches")

                return True


def get_filename(fname):

    filename = os.path.basename(fname)

    return filename


def get_filesize(fname):

    stat_info = os.stat(fname)

    file_size = stat_info.st_size

    return file_size


对于文件名,它通过循环,但打印出 3 个不匹配的No matches和一个All files matches都应该匹配的。然后对于文件大小和get_md5,我得到OSError: [Errno 2] No such file or directory: 'text3.txt'

函数式编程
浏览 162回答 1
1回答

RISEBY

而不是这个:for (dirpath, dirnames, filenames) in os.walk(path):&nbsp; &nbsp; for files in filenames:&nbsp; &nbsp; &nbsp; &nbsp; if (dic.get('Directory') == path&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and dic.get('Filename') == get_filename(files)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and dic.get('File Size') == get_filesize(files)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and dic.get('Hash') == get_md5(files)):你应该使用过:for root, dirs, files in os.walk(path):&nbsp; &nbsp; for f in files:&nbsp; &nbsp; &nbsp; &nbsp; file_name = os.path.join( root, f )&nbsp; &nbsp;# <<--- this is important&nbsp; &nbsp; &nbsp; &nbsp; if (dic.get('Directory') == path&nbsp; &nbsp; &nbsp; # `root` here, not `path` ??&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and dic.get('Filename') == get_filename(file_name)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and dic.get('File Size') == get_filesize(file_name)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and dic.get('Hash') == get_md5(file_name)):
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python