72

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.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880

3 Answers3

141
>>> import os
>>> f = os.path.getmtime('test1.jpg')
>>> f
1223995325.0

since the beginning of (epoch)

Jack
  • 20,735
  • 11
  • 48
  • 48
75

os.stat()

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).

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