Categories
Python Answers

How to get file creation and modification date/times with Python?

Spread the love

Sometimes, we want to get file creation and modification date/times with Python.

In this article, we’ll look at how to get file creation and modification date/times with Python.

How to get file creation and modification date/times with Python?

To get file creation and modification date/times with Python, we can use the getmtime and getctime methods.

For instance, we write

import os.path, time

print("last modified: %s" % time.ctime(os.path.getmtime(file)))
print("created: %s" % time.ctime(os.path.getctime(file)))

to get the last modified datetime of file with os.path.getmtime.

Likewise, we use os.path.getctime to get the file‘s created datetime.

Conclusion

To get file creation and modification date/times with Python, we can use the getmtime and getctime methods.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *