I'm having trouble using os.utime
to correctly set the modification time on the mac (Mac OS X 10.6.2, running Python 2.6.1 from /usr/bin/python
). It's not consistent with the touch
utility, and it's not consistent with the properties displayed in the Finder's "get info" window.
Consider the following command sequence. The 'created' and 'modified' times in the plain text refer to the attributes shown in the "get info" window in the finder. As a reminder, os.utime takes arguments (filename, (atime, mtime))
.
>>> import os
>>> open('tempfile','w').close()
'created' and 'modified' are both the current time.
>>> os.utime('tempfile', (1000000000, 1500000000) )
'created' is the current time, 'modified' is July 13, 2017.
>>> os.utime('tempfile', (1000000000, 1000000000) )
'created' and 'modified' are both September 8, 2001.
>>> os.path.getmtime('tempfile')
1000000000.0
>>> os.path.getctime('tempfile')
1269021939.0
>>> os.path.getatime('tempfile')
1269021951.0
...but the os.path.get?time
and os.stat
don't reflect it.
>>> os.utime('tempfile', (1500000000, 1000000000) )
'created' and 'modified' are still both September 8, 2001.
>>> os.utime('tempfile', (1500000000, 1500000000) )
'created' is September 8, 2001, 'modified' is July 13, 2017.
I'm not sure if this is a Python problem or a Mac stat problem. When I exit the Python shell and run
touch -a -t 200011221234 tempfile
neither the modification nor the creation times are changed, as expected. Then I run
touch -m -t 200011221234 tempfile
and both 'created' and 'modified' times are changed.
Does anyone have any idea what's going on? How do I change the modification and creation times consistently on the mac? (Yes, I am aware that on Unixy systems there is no "creation time.")
Result from running Chris Johnsen's script:
seth@local:~$ /usr/bin/python timetest.py tempfile 5
initial:
(1269631281.0, 1269631281.0, 1269631281.0, 1269631281, 1269631281, 1269631281)
test: (1000000000, 1000000000)
(1000000000.0, 1000000000.0, 1269631281.0, 1000000000, 1000000000, 1269631281)
(1269631281.0, 1000000000.0, 1269631281.0, 1269631281, 1000000000, 1269631281)
test: (1000000000, 1500000000)
(1000000000.0, 1500000000.0, 1269631286.0, 1000000000, 1500000000, 1269631286)
(1269631286.0, 1500000000.0, 1269631286.0, 1269631286, 1500000000, 1269631286)
test: (1500000000, 1000000000)
(1500000000.0, 1000000000.0, 1269631291.0, 1500000000, 1000000000, 1269631291)
(1269631291.0, 1000000000.0, 1269631291.0, 1269631291, 1000000000, 1269631291)
test: (1500000000, 1500000000)
(1500000000.0, 1500000000.0, 1269631296.0, 1500000000, 1500000000, 1269631296)
(1269631296.0, 1500000000.0, 1269631296.0, 1269631296, 1500000000, 1269631296)
At the end of the exercise, the 'created' date as visible in the finder is 9/8/01 and the 'modified' date is 7/13/17. (The access date, thanks to presumably spotlight as you suggest and as I've read about, is roughly 'now.') The created and modified dates visible in the finder still make no sense.