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.
Asked
Active
Viewed 8.6k times
72

Bill the Lizard
- 398,270
- 210
- 566
- 880
3 Answers
141
>>> import os
>>> f = os.path.getmtime('test1.jpg')
>>> f
1223995325.0
since the beginning of (epoch)

Jack
- 20,735
- 11
- 48
- 48
75
import os
filename = "/etc/fstab"
statbuf = os.stat(filename)
print("Modification time: {}".format(statbuf.st_mtime))
Linux does not record the creation time of a file (for most fileystems).

dowhilegeek
- 7
- 2

Douglas Leeder
- 52,368
- 9
- 94
- 137
54
New for python 3.4+ (see: pathlib)
import pathlib
path = Path('some/path/to/file.ext')
last_modified = path.stat().st_mtime

Brian Bruggeman
- 5,008
- 2
- 36
- 55