tags:

views:

738

answers:

2

Assuming the file exists (using os.path.exists(filename) to first make sure that it does), how do I display the time a file was last modified? This is on Linux if that makes any difference.

+6  A: 

os.stat()

import os
filename = "/etc/fstab"
statbuf = os.stat(filename)
print "Modification time:",statbuf.st_mtime

Unfortunately I don't think Linux records the creation time. (At least it isn't available from os.stat).

Douglas Leeder
Thank you. It appears that Linux doesn't store the file creation time. It seems like I should have known that. :)
Bill the Lizard
+10  A: 
>>> import os
>>> f = os.path.getmtime('test1.jpg')
>>> f
1223995325.0

since the beginning of (epoch)

Jack
Thanks, this was helpful. This seems to be the more direct approach to my specific question, but the os.stat solution gives more information.
Bill the Lizard
I didn't know that there was an explicit function for this. Live and learn I guess.
Douglas Leeder
Me neither; time to replace `os.stat` with `os.path.getmtime` in my codebase...
Sridhar Ratnakumar