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
また、 2 つの異なるファイルから取得したタイムスタンプを比較せずに、より少ないコードでこの動作を再現することもできませんでした。ここで些細なことを見逃しているような気がします...
編集:さらにテストした結果、たまに印刷されることがわかり
True
ましたが、10回の実行ごとに1回以上は印刷されません。
編集 2: larsmans の提案によると:
print ("%.7f" % t1) # prints e.g. 1371126279.1365688
print ("%.7f" % t2) # prints e.g. 1371126279.1365681
これにより、次の 2 つの新しい疑問が生じます。
- を呼び出した後、タイムスタンプが等しくないのはなぜ
shutil.copystat
ですか? print
デフォルトでフロートを丸めますか?!