File Modification Times Not Equal After Calling Shutil.copystat(file1, File2) Under Windows
I run the following code with Python 2.7.5. under 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?!
I expect both timestamps (=floats) to be equal (as their string representations suggest), so why does
t1 == t2 evaluate to
Also, I was unable to reproduce this behaviour with less code, i.e. without comparing the timestamps retrieved via
os.lstat from two different files. I have the feeling, I am missing something trivial here...
Edit: After further testing I noticed, that it does print
Trueonce in a while, but not more often than once every 10 runs.
Edit 2: As suggested by larsmans:
print ("%.7f" % t1) # prints e.g. 1371126279.1365688 print ("%.7f" % t2) # prints e.g. 1371126279.1365681
This raises two new questions:
- Why are the timestamps not equal after calling
The problem is with conversion between different formats during the
copystat call. This is because Windows stores file times in a fixed-point decimal format, while Python stores them in a floating-point binary format. So each time there is a conversion between the two formats, some accuracy is lost. During the
- A call to
os.statconverts the Windows format to Python's floating-point format. Some accuracy is lost.
os.utimeis called to update the file time. this converts it back to the Windows format. Some accuracy is lost again, and the file time is not necessarily the same as the first file's.
When you call
os.lstat yourself, a third inaccurate conversion is performed. Due to these conversions, the file times are not exactly the same.
The documentation for
os.utime mentions this:
Note that the exact times you set here may not be returned by a subsequent stat() call, depending on the resolution with which your operating system records access and modification times
Regarding your second question (why
print f will round the value. To get a value guaranteed to be unique for different floating-point values, use
print repr(f) instead.
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module