Sometimes, we want to get file creation and modification date and times in Python.
In this article, we’ll look at how to get file creation and modification date and times in Python.
How to get file creation and modification date and times in Python?
To get file creation and modification date and times in Python, we can use the os.path.getctime
and os.path.getmtime
methods respectively.
For instance, we write:
import os.path, time
file = './bar.txt'
print("last modified: %s" % time.ctime(os.path.getmtime(file)))
print("created: %s" % time.ctime(os.path.getctime(file)))
We call time.ctime
with os.path.getmtime
to convert the Unix timestamp of the modification date and time of the file
to a human readable date and time string.
Likewise, we call time.ctime
with os.path.getctime
to convert the Unix timestamp of the creation date and time of the file
to a human readable date and time string.
Therefore, we should see something like:
last modified: Sun Oct 17 17:45:30 2021
created: Sun Oct 17 17:50:44 2021
printed from the print
calls.
Conclusion
To get file creation and modification date and times in Python, we can use the os.path.getctime
and os.path.getmtime
methods respectively.