Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python get time stamp on file in mm/dd/yyyy format

Tags:

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

like image 775
Ank Avatar asked Jun 08 '13 00:06

Ank


People also ask

How do I get the timestamp of a file in Python?

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).

How do I print a date in dd-mm-yyyy format in Python?

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.


1 Answers

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))) 
like image 91
Martijn Pieters Avatar answered Sep 19 '22 01:09

Martijn Pieters