I'm trying to get the datestamp on the file in mm/dd/yyyy format
time.ctime(os.path.getmtime(file))
gives me detailed time stamp Fri Jun 07 16:54:31 2013
How can I display the output as 06/07/2013
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).
Use strftime() function of a datetime class The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.
You want to use time.strftime()
to format the timestamp; convert it to a time tuple first using either time.gmtime()
or time.localtime()
:
time.strftime('%m/%d/%Y', time.gmtime(os.path.getmtime(file)))
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