23

How do I get the modified date/time of a file in Python?

Torsten Marek
  • 79,210
  • 20
  • 90
  • 98
Matt
  • 78,399
  • 25
  • 54
  • 67

2 Answers2

39
os.path.getmtime(filepath)

or

os.stat(filepath).st_mtime
Thomas Wouters
  • 125,217
  • 23
  • 144
  • 122
  • 1
    @endolith: http://stackoverflow.com/a/237082/125507 says "os.path.getmtime() is made for this, and simpler." – endolith Mar 01 '12 at 15:19
19

Formated:

import time
print time.strftime("%m/%d/%Y %I:%M:%S %p",time.localtime(os.path.getmtime(fname)))
Dingo
  • 2,486
  • 1
  • 18
  • 14