【发布时间】: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