[Tutor] datetime substraction

Steven D'Aprano steve at pearwood.info
Tue Aug 26 18:47:34 CEST 2014


On Tue, Aug 26, 2014 at 02:40:30PM +0530, Anirudh Tamsekar wrote:

> I have written a script, however I'm not able to get the date substraction
> math right, getting the following error
> (Searched google and other resources too).
> 
> *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'*

cur_time and mt are strings. You cannot subtract strings.

os.path.getctime(filename) returns the ctime of the file as a number. 
You then convert that number into a string. Don't. Just leave it as a 
number:

mt = os.path.getctime(tfile)
cur_time = time.time()
difft = cur_time - mt


-- 
Steven


More information about the Tutor mailing list