首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查文件完整性失败

检查文件完整性失败
EN

Stack Overflow用户
提问于 2012-10-25 18:44:49
回答 1查看 940关注 0票数 0

我正在编写一个快速的Python脚本,用于将文件从一个目录迁移到另一个目录。除了我必须比较文件名和校验和的部分之外,一切都运行得很好。我将文件存储在两个位置,即/root/src/file1和/root/dst/file1。

因此,当我运行文件名比较时,它无法匹配文件,因为它包含了整个文件路径。md5Srt是一个存储文件和校验和的字典。

有没有办法在不使用整个文件路径的情况下比较文件名?

代码语言:javascript
复制
for key in md5Srt.keys():
    if key in md5Dst:
        print "keys match " + key
        print '\ncomparing the values of files\n'
        if md5Srt[key] == md5Dst[key]:
            print md5Srt[key]
            print md5Dst[key]
            print "files match\n"
            print "checking the next pair"
        else:
            print "values of files don't match"
EN

回答 1

Stack Overflow用户

发布于 2012-10-25 23:12:02

如果在一个目录中只有一堆文件,那么可以使用os.path.basename

代码语言:javascript
复制
import os
>>> dst = os.path.basename('/root/dst/file1.file')
>>> src =  os.path.basename('/root/src/file1.file')
>>> dst
'file1.file'
>>> src
'file1.file'
>>> dst == src
True

如果要处理子目录,则需要知道基本src和dst目录,然后从每个路径的开头删除它们:

代码语言:javascript
复制
>>> src = '/root/src'
>>> dst = '/root/dst'
>>> src_file = '/root/src/dir1/file1.file'
>>> dst_file = '/root/dst/dir1/file1.file'
>>> os.path.relpath(src_file, src)
'dir1/file1.file'
>>> os.path.relpath(dst_file, dst)
'dir1/file1.file'
>>> os.path.relpath(src_file, src) == os.path.relpath(dst_file, dst)
True

如果你把它和你的函数结合起来,你会得到:

代码语言:javascript
复制
import os

src = '/root/src'
dst = '/root/dst'
for key, src_file in md5Srt.iteritems():
    dst_file = md5Dst.get(key)
    if dst_file is None:
        print 'The destination is missing %s' src_file
        continue

    print "keys match " + key
    print '\ncomparing the values of files\n'
    if  os.path.relpath(src_file, src) == os.path.relpath(dst_file, dst)
            print srcFile
            print dst_file
            print "files match\n"
            print "checking the next pair"
    else:
            print "values of files don't match"

我认为您应该重新考虑通过在dst中查找与src中的文件具有相同md5sum的文件来尝试比较文件。如果一个文件被重命名,或者有两个文件具有相同的散列,那么最终得到的目录可能并不完全相同。一种更好的方法是首先比较文件名,如果有一个文件同时在srcdst中,则检查md5sums。

下面是它可能的样子:

代码语言:javascript
复制
import os

src_dir = '/root/src'
dst_dir = '/root/dst'

# reverse the dictionaries, hopefully you would create these dictionaries 
# to begin with. A single file can only have one md5sum, but the same md5Sum can 
# match multiple files
src_file_hashes = dict((os.path.relpath(v, src_dir), k) for k, v in md5Srt)
dst_file_hashes = dict((os.path.relpath(v, dst_dir), k) for k, v in md5Dst)

for src_file, src_hash in src_file_hashes.iteritems():
    dst_hash = dst_file_hashes.get(src_file)

    src_path = os.path.join(src_dir, src_file)
    dst_path = os.path.join(dst_dir, dst_file)

    if dst_hash is None:
        print 'The destination file %s is missing ' % dst_path
        continue

    if  src_hash == dst_hash:
        print '%s matches %s and %s' % (src_hash, src_path, dst_path)
    else:
        print '%s and %s have different hashes' % (src_path, dst_path)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13066862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档