How to Get the Creation Date of a File in Python



Python


In this article, we show how to get the creation date of a file in Python.

The creation date of a file is the time it was created.

Other times such as last modified is the time the file was last modified (edited).

So the first thing we have to do is import the os module into the script.

We use this module to retrieve a file and then display certain attributes. In the case of this program, we are going to retrieve the file's creation date.

So below is the script that allows us to obtain the creation date of a file named file.txt.



So this works.

It returns the creation date of a file. However, the time is returned as a timestamp, which may be easy for computers to understand but that aren't very human readable.

Thus, more than likely, you want to return a more human readable version. Therefore, you would want to convert this timestamp into datetime. Timestamp isn't very human readable; datetime is.

Therefore, below, we show the code where we return the creation date of a file in datetime.



So now we have the last modified date of the file as a datetime, which is a lot more human readable than a timestamp.

And this is all that is required to get the creation date of a file in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...