23

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

Torsten Marek
  • 83,780
  • 21
  • 91
  • 98
Matt
  • 84,419
  • 25
  • 57
  • 67

2 Answers2

39
os.path.getmtime(filepath)

or

os.stat(filepath).st_mtime
Thomas Wouters
  • 130,178
  • 23
  • 148
  • 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
20

Formated:

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