I have his line in python. I would like it to output month and year as MM/YYYY. What is the best way to do this.
time.ctime(os.path.getmtime(os.path.join(dir_path, file_name)))
Current output:
Wed Sep 26 08:57:26 2012
I would like 9/2012
I have his line in python. I would like it to output month and year as MM/YYYY. What is the best way to do this.
time.ctime(os.path.getmtime(os.path.join(dir_path, file_name)))
Current output:
Wed Sep 26 08:57:26 2012
I would like 9/2012
time.ctime is documented to return a "string representing local time". It doesn't say what that string is (or how it's formatted), so I assume it's probably platform specific.
Instead, you'll probably want to use datetime.
secs = os.path.getmtime(os.path.join(dir_path, file_name))
date = datetime.date.fromtimestamp(secs)
formatted = date.strftime('%m/%Y')
ts = os.path.getmtime(os.path.join(dir_path, file_name))
print(time.strftime('%m/%Y', time.gmtime(ts)))
time.localtime(ts) instead of time.gmtime(ts).