Python - How to get the file last modified date

By xngo on June 12, 2019

In Python, you can use the os.path.getmtime(path) to get the file’s last modified timestamps. This method will returns the time in seconds since the epoch.

import os
import datetime
 
last_modified_sec = os.path.getmtime('wildlife.jpg')
last_modified_date = datetime.datetime.fromtimestamp(last_modified_sec)
 
print("Last modified in seconds:", last_modified_sec)
print("Last modified date:", last_modified_date)

Output

Last modified in seconds: 1560368140.686942
Last modified date: 2019-06-12 15:35:40.686942

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.