【问题标题】:Comparing files, path to check filenames, filesize, and checksum md5 in python在 python 中比较文件、检查文件名、文件大小和校验和 md5 的路径
【发布时间】:2019-09-08 21:54:17
【问题描述】:

我想比较具有路径、文件名、文件大小和 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

def get_md5(fname):
    hash_md5 = hashlib.md5()
    with open(fname, "rb") as f:
        for chunk in iter(lambda: f.read(2 ** 20), b""):
            hash_md5.update(chunk)
            get_hash = hash_md5.hexdigest()
            return get_hash

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

对于文件名问题:

{'Directory': '/var/tmp/test', 'File Size': '257', 'Hash': '2e6041635f72233f4cdf6fbfb0a8288e', 'Filename': 'test1.txt'}
{'Directory': '/var/tmp/test', 'File Size': '68', 'Hash': 'd3428d5910f54270d62ff57ccd5ff52c', 'Filename': 'text2.txt'}
{'Directory': '/var/tmp/test', 'File Size': '58', 'Hash': '42e8b3cba5320e07745110b8b193f534', 'Filename': 'text3.txt'}
{'Directory': '/var/tmp/test', 'File Size': '128', 'Hash': '4acc96e6e8b9006722408e15e555d2c2', 'Filename': 'text4.xml'}
{'Directory': '/var/tmp/test', 'File Size': '214', 'Hash': 'a7071c13195d8485b2fb4a68503cbd7a', 'Filename': 'text5.csv'}
No matches
No matches
No matches
All files matches

对于文件大小:

  File "./create_manifest.py", line 44, in csv_checksum
    comp_baseline_manifest(csvDics, path)
  File "./create_manifest.py", line 88, in comp_baseline_manifest
    and dic.get('File Size') == get_filesize(files)):
  File "./create_manifest.py", line 100, in get_filesize
    stat_info = os.stat(fname)
OSError: [Errno 2] No such file or directory: 'text3.txt'

对于md5错误:

    comp_baseline_manifest(csvDics, path)
  File "./create_manifest.py", line 89, in comp_baseline_manifest
    and dic.get('Hash') == get_md5(files)):
  File "./create_manifest.py", line 107, in get_md5
    with open(fname, "rb") as f:
IOError: [Errno 2] No such file or directory: 'text3.txt'

【问题讨论】:

  • 不仔细看,可能file.txt的路径不对吧?您可以添加诊断打印,显示正在传递给文件大小和 md5 函数的内容。
  • 好吧,对不起,我错过了输入那部分,实际上是 text3.txt 抱怨,如果我在终端中检查该文件,它的 md5sum 为 md5sum text3.txt 与 csv 文件相同,并且它在正确的目录。对于文件大小,如果我以 ls -lh text3.txt 的身份进入终端,我得到的大小与 58 相同文件名、文件大小和 md5 问题。
  • 在检查文件大小和 md5 时应使用文件名加入目录并使用完整路径
  • @furas 好的,我以and dic.get('File Size') == get_filesize(os.path.join(path, files)) and and dic.get('Hash') == get_md5(os.path.join(path, files))): 运行它,我得到以下信息:``` 不匹配 不匹配 不匹配 214 不匹配 不匹配```
  • 也许你不应该为for-loop 中的每个检查文件写No mathes,但你应该在结束for 循环后只写一次。

标签: python python-2.7 filenames checksum filesize


【解决方案1】:

而不是这个:

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

你应该用过:

for root, dirs, files in os.walk(path):
    for f in files:
        file_name = os.path.join( root, f )   # <<--- this is important
        if (dic.get('Directory') == path      # `root` here, not `path` ??
                and dic.get('Filename') == get_filename(file_name)
                and dic.get('File Size') == get_filesize(file_name)
                and dic.get('Hash') == get_md5(file_name)):

【讨论】:

  • 谢谢@Ienik,它肯定会采用该路径,但结果显示No matches,我看到我只得到一个文件大小的输出No matches: /var/tmp/test/text3.txt No matches: /var/tmp/test/text4.xml No matches: /var/tmp/test/test1.txt All files matches: /var/tmp/test/text5.csv 所有人都应该说All files matches,因为所有文件都是在该目录文件夹中相同
  • 我没有你的文件,所以我无法检查自己。明确打印从 .csv 读取的数据和从实际文件(大小、md5 等)获得的数据并查看是否有任何差异可能是个好主意。
  • 是的,我正在这样做,似乎 csv 中的输出很奇怪,不确定排序 dic 是否正确,但不确定如何让这本字典正常工作。我粘贴了上面的结果,这里是没有排序的输出:{'Hash': 'a7071c13195d8485b2fb4a68503cbd7a', 'Filename': 'text5.csv', 'File Size': '214', 'Directory': '/var/tmp/test'} 它只输出一个文件,这就是问题所在。不知道如何解决它,我试图将 comp_original(csvDics, path) 放在字典结束但没有运气的 csv_checksum(files, path) 函数的 for 循环中。
  • 是的,正如上面提到的 furas 一样,我如何循环这些消息以获得不匹配可能是个问题。所有文件都与上面 csv 文件中描述的相同,我已经检查过了,csv 文件的输出看起来也很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多