I am trying to get a the modified date of a file using os.path.getmtime()
and to convert time into human readable format; it is advised to use time.ctime()
method but it gives the name of the date and month too, which I do not want.
Is it possible to format %d %m %Y, %H:%M
?
Currently I have code like:
import os.path, time
path = #path of the file
print time.ctime(os.path.getmtime(path))
You can do that with datetime.fromtimestamp() like:
file_time_string = dt.datetime.fromtimestamp(os.path.getmtime(__file__))
import os
import datetime as dt
file_time = dt.datetime.fromtimestamp(os.path.getmtime(__file__))
print(file_time.strftime("%d %m %Y, %H:%M"))
24 03 2018, 16:39
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