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

python get time stamp on file in mm/dd/yyyy 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)))


from datetime import datetimefrom os.path import getmtimedatetime.fromtimestamp(getmtime(file)).strftime('%m/%d/%Y')


You can create the datetime object using the ctime str like you mention and then format it back to a string of any format.

str1 = time.ctime(os.path.getmtime(file)) # Fri Jun 07 16:54:31 2013datetime_object = datetime.strptime(str1, '%a %b %d %H:%M:%S %Y')datetime_object.strftime("%m/%d/%Y") # 06/07/2013

This way you don't have to deal with timezones + absolute timestamp from the Epoch

Credit: Converting string into datetime

Linking: How to get file creation & modification date/times in Python?

http://strftime.org/