[Tutor] datetime substraction

Alan Gauld alan.gauld at btinternet.com
Tue Aug 26 18:55:10 CEST 2014


On 26/08/14 10:10, Anirudh Tamsekar wrote:

> *Traceback (most recent call last):*
> *  File "./rsyslog_check.py", line 22, in <module>*
> *    difft=cur_time-mt*
> *TypeError: unsupported operand type(s) for -: 'str' and 'str'*
>
> Request your help. Also point any good documentation where I can get
> quick reference.
>
> ct = time.ctime(os.path.getctime(tfile))
> mt = time.ctime(os.path.getctime(tfile))

You are converting the times to strings before you subtract them. Don't 
do that, subtract the times as returned by os.path.getctime() and 
time.time().

> cur_time = time.ctime()
> difft=int(cur_time-mt)

You need to subtract the numeric versions of the times not the strings.

The best quick references are the dir() and help() builtin functions.
For more detail read the documentation for time, os.path (and maybe 
datetime) modules.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list