How do I get the modified date/time of a file in Python?
Using time module to get file creation & modification date or time in Python. We will use getctime() and getmtime() function, found inside the path module in the os library, for getting the creation and modification times of the file.
Use the cd command to access the directory containing the files you want to see the modification date. Then, use the ls command to list all files in the current directory. We recommend using the ls command below to give you the best listing of files, including the modification date.
Use getctime() function to get a creation timepath. getmtime('file_path') function returns the creation time of a file. On the other hand, on Unix, it will not work. Instead, it will return the timestamp of the last time when the file's attributes or content were changed (most recent metadata change on Unix).
The easiest way to get the file creation date is with the stat command. As we can see, the creation date is shown in the “Birth” field.
os.path.getmtime(filepath)
or
os.stat(filepath).st_mtime
Formated:
import time print time.strftime("%m/%d/%Y %I:%M:%S %p",time.localtime(os.path.getmtime(fname)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With