Question

The issue I am running into is part of using os.stat on a path (Take C:\myfile1.txt for example). When I run os.stat on this file and take the 9th element in the resulting list I get the modified time in the form of some numbers (ex. 1348167977).

NOTE: I'm not sure how these numbers are calculated.

When I create C:\myfile1.txt it has some number like the example above. If I create another file C:\myfile2.txt, it gets a new number representing the modified time which is higher than C:\myfile1.txt (this is like I would expect). I also have a third file C:\myfile3.txt which is created last.

The issue comes if I copy C:\myfile2.txt and overwrite C:\myfile3.txt with the resulting copy file, the modified time as shown by os.stat on the new C:\myfile3.txt is less than C:\myfile1.txt. Why does this happen? The modified time for C:\myfile3.txt should be the highest of all or at least equal to C:\myfile2.txt.

Thanks for you answers, I hope I explained this well enough.

EDIT:

Here's some sample code to test what I describe. Sometimes it works sometimes the numbers are all the same if you rerun it at a different time. I think I just don't fully understand the MTIME that I'm outputting.

import os
import shutil
import time

myfile1 = open("C:\\myfile1.txt", 'wt')
myfile1.close()
time.sleep(10)
myfile2 = open("C:\\myfile2.txt", 'wt')
myfile2.close()
time.sleep(10)
myfile2 = open("C:\\myfile3.txt", 'wt')
myfile2.close()

shutil.copyfile("C:\\myfile2.txt", "C:\\myfile3.txt")

modified_time_first = (os.stat("C:\\myfile1.txt")[9])
modified_time_second = (os.stat("C:\\myfile2.txt")[9])
modified_time_third = (os.stat("C:\\myfile3.txt")[9])

print "The first files modified time is: "
print modified_time_first
print ""

print "The second files modified time is: " 
print modified_time_second
print ""

print "The third files modified time is: " 
print modified_time_third
print ""
Was it helpful?

Solution

The number you see is a UNIX timestamp, and represents the number of seconds since the epoch, the 1st of January 1970. You could use datetime.datetime.fromtimestamp() to turn that into a python datetime object for example:

>>> import datetime
>>> datetime.datetime.fromtimestamp(1348167977)
datetime.datetime(2012, 9, 20, 21, 6, 17)

Note that os.stat() returns a named tuple; you can access the properties on that tuple by name too:

>>> import os
>>> os.stat('test.txt').st_mtime
1348169795.0

See the os.stat() documentation for all the attributes that are available.

You, on the other hand, accessed the st_ctime value instead; python tuples and list indexes start at 0, so the 9th element is index 8. You accessed index 10, the creation time of the files.

If all you need is the modified time of a file, you can also use os.path.getmtime() as a convenient shortcut:

>>> os.path.getmtime('test.txt')
1348169795.0
>>> datetime.datetime.fromtimestamp(os.path.getmtime('test.txt'))
datetime.datetime(2012, 9, 20, 21, 36, 35)

When copying a file, it's properties such as last-modification timestamp are usually included, thus the modified time of the newly copied file could easily change, both backwards and forwards. The same applies to the creation time.

OTHER TIPS

Offhand, the string appears to be in "epoch time" format. I use ipython:

import os
import time
time.ctime(os.stat('index.php')[9])
==>'Fri Jun 15 23:05:07 2012'

I use Mac OS X, so I did a man stat. It appears that the 9th item returned by stat is the "ctime." "ctime", in the *nix world refers to the inode data modification information. You probably could test out whether or not this field should be modified performing the operation you are doing or not. You could likely do this by hand, and running stat on the files generated to confirm if this should change. You might be picking the wrong part of os.stat.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top