How to Get the Last Modified Date of a File in Python 


               


How to Get the Last Modified Date of a File in Python



Python


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

We can do this using the Python os module.

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 last modified date.

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



So this works.

It returns the last modified 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 last modified 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 last modified 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...