这个问题是关于python2.7.1中的^{True
,另一个返回False
。在
>>> filecmp.cmp(r'h:\dcim\112_1029\imgp7258.dng', r'd:\pictures\2016\112_1029\imgp7258.dng', False)
True
>>> filecmp.cmp('h:\\dcim\\112_1029\\IMGP7258.DNG', 'd:\\pictures\\2016\\112_1029\\IMGP7258.DNG', False)
False
h:
是SD卡,d:
是标准硬盘。我已经通过Explorer将文件从h:
复制到d:
,因此它们应该是相同的。我甚至再做一次只是为了确定。不管我打了多少次电话,或者我按什么顺序打电话,结果都是一致的。在
这里有更多的实验只是为了进一步混淆。在
^{pr2}$按照注释中的要求,下面是4个文件名中每个文件名的os.stat
的结果。您可以看到,除了访问和创建时间之外,它们是相同的,但是对于每个版本的文件名,这些时间是一致的。在
>>> os.stat(f1)
nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=15112724L, st_atime=1490418000L, st_mtime=1477766688L, st_ctime=1477766686L)
>>> os.stat(f2)
nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=15112724L, st_atime=1490488519L, st_mtime=1477766688L, st_ctime=1490488519L)
>>> os.stat(f3)
nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=15112724L, st_atime=1490418000L, st_mtime=1477766688L, st_ctime=1477766686L)
>>> os.stat(f4)
nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=15112724L, st_atime=1490488519L, st_mtime=1477766688L, st_ctime=1490488519L)
我想到了一个额外的测试:
>>> def hashfile(filename):
m = hashlib.md5()
with open(filename, 'rb') as f:
m.update(f.read())
return ''.join('%02x' % ord(b) for b in m.digest())
>>> hashfile(f1)
'a0042d8623567bcf429069d17e7c3148'
>>> hashfile(f2)
'a0042d8623567bcf429069d17e7c3148'
>>> hashfile(f3)
'a0042d8623567bcf429069d17e7c3148'
>>> hashfile(f4)
'a0042d8623567bcf429069d17e7c3148'
>>> filecmp.cmp(f1, f2, False)
True
>>> filecmp.cmp(f3, f4, False)
False
documentation for 2.7没有提到它,但是documentation for 3.4包含了一个非常重要的注释:
缓存似乎对文件名区分大小写。在
在我的例子中,我重新复制了文件,因此
os.stat()
信息没有改变(Windows保留这些属性)。然而,当我在不同的情况下使用文件名进行测试时,我得到的未缓存结果与缓存的结果不一样。在按照文档中的建议调用
clear_cache()
是解决我问题的确切方法。在相关问题 更多 >
编程相关推荐