Reputation: 1
By using:
import os,os.path,time,shutil,datetime
print time.ctime(os.path.getmtime("/home/sulata/Documents/source/"))
print time.ctime(os.path.getmtime("/home/sulata/Documents/destination/"))
I'm getting the outputs:
Mon Apr 2 15:56:00 2018
Mon Apr 2 15:56:03 2018
I want to get the time without seconds.
Upvotes: 0
Views: 1709
Reputation: 917
The general method is:
import time
time.strftime(format)
example:
>>>time.strftime("%H:%M:%S")
20:08:40
In your case:
>>> time.strftime("%H:%M")
13:41
>>>time.strftime("%a %b %d %H:%M %Y")
'Mon Apr 02 13:27 2018'
if you want to remove the Zero, you could do something like this...
>>> time.strftime("%a %b "+str(int(time.strftime("%d"))) +" %H:%M %Y")
'Mon Apr 2 13:33 2018'
Upvotes: 2
Reputation: 532
You can try this -
>>> import time, os
>>>
>>> x = time.gmtime(os.path.getmtime('/home/abhi/test.file'))
>>>
>>> x
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=2, tm_hour=11, tm_min=19, tm_sec=43, tm_wday=0, tm_yday=92, tm_isdst=0)
>>> x.tm_sec
43
>>> x.tm_hour
11
>>>
Upvotes: 0
Reputation: 18106
getmtime
returns a timestamp, so you can format it using strftime
to whatever format you need:
>>> import datetime as dt
>>> import os
>>> mTime = os.path.getmtime("/tmp/xauth-1000-_0")
>>> dt.datetime.fromtimestamp(mTime).strftime("%Y-%m-%d %H:%M")
'2018-04-01 22:07'
Format identifiers can be found in the docs.
Upvotes: 0
Reputation: 3553
JulioCamPlaz has already provided a decent enough solution, but if you want to maintain the date format, you can use regex for this:
import re
x = time.ctime(os.path.getmtime("/home/sulata/Documents/source/"))
print(re.sub(r':\d{1,2}\s', ' ', x))
What this does is that it removes the final two digits (seconds) of the time which are followed by a space, and replaces it with a space.
Although this may be an over-complicated method, it is short, and gives you the exact same date format without the seconds, and without altering it in any way.
Upvotes: 0
Reputation: 82765
Use datetime to get the seconds.
Ex:
import os,os.path,time,shutil,datetime
print datetime.datetime.strptime(time.ctime(os.path.getmtime(r"/home/sulata/Documents/source/")), "%a %b %d %H:%M:%S %Y").second
Upvotes: 0