相关疑难解决方法(0)

filecmp.cmp()忽略不同的os.stat()签名?

Python 2文档filecmp()说:

除非给出并且为假,否则具有相同os.stat()签名的文件被认为是相等的.

这听起来像是两个相同的文件,除了它们的os.stat()签名之外都会被认为是不相等的,但是这似乎不是这种情况,如运行以下代码片段所示:

import filecmp
import os
import shutil
import time

with open('test_file_1', 'w') as f:
    f.write('file contents')
shutil.copy('test_file_1', 'test_file_2')
time.sleep(5)  # pause to get a different time-stamp
os.utime('test_file_2', None)  # change copied file's time-stamp

print 'test_file_1:', os.stat('test_file_1')
print 'test_file_2:', os.stat('test_file_2')
print 'filecmp.cmp():', filecmp.cmp('test_file_1', 'test_file_2')
Run Code Online (Sandbox Code Playgroud)

输出:

test_file_1: nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0,
  st_uid=0, st_gid=0, st_size=13L, st_atime=1320719522L, st_mtime=1320720444L, 
  st_ctime=1320719522L)
test_file_2: nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, 
  st_uid=0, st_gid=0, st_size=13L, st_atime=1320720504L, st_mtime=1320720504L, 
  st_ctime=1320719539L)
filecmp.cmp(): True
Run Code Online (Sandbox Code Playgroud)

正如你可以看到两个文件的时间戳 …

python file-comparison

5
推荐指数
1
解决办法
2812
查看次数

标签 统计

file-comparison ×1

python ×1