我使用Python 2.7.5运行以下代码。在Windows下:
import os, shutil, stat, time
with open('test.txt', 'w') as f: pass # create an arbitrary file
shutil.copy('test.txt', 'test2.txt') # copy it
shutil.copystat('test.txt', 'test2.txt') # copy its stats, too
t1 = os.lstat('test.txt').st_mtime # get the time of last modification for both files
t2 = os.lstat('test2.txt').st_mtime
print t1 # prints something like: 1371123658.54
print t2 # prints the same string, as expected: 1371123658.54
print t1 == t2 # prints False! Why?!
我希望两个时间戳(= floats)都相等(如其字符串表示所暗示的),那么为什么t1 == t2求和为False?
另外,我无法用更少的代码来重现此行为,即,如果不比较os.lstat从两个不同文件中检索到的时间戳。我有种感觉,我在这里错过了一些琐碎的事情...
经过进一步测试,我注意到它确实会True偶尔打印一次,但不会经常每10次运行一次。
如larsmans所建议:
print ("%.7f" % t1) # prints e.g. 1371126279.1365688
print ("%.7f" % t2) # prints e.g. 1371126279.1365681
这就提出了两个新的问题:
为什么呼叫后时间戳不相等shutil.copystat?
print 默认情况下,四舍五入为浮点数?!
HUH函数
杨魅力
慕侠2389804
蓝山帝景
随时随地看视频慕课网APP
相关分类