#!/usr/bin/python3.6 import datetime with open("/tmp/1", "r") as f: for line in f: a = {'second': 0, 'minute': 0, 'hour': 0, 'day': 0} result = "" time1 = datetime.datetime.strptime(line.split()[0] + " " + line.split()[1], "%m/%d/%Y %H:%M:%S") time2 = datetime.datetime.strptime(line.split()[2] + " " + line.split()[3], "%m/%d/%Y %H:%M:%S") timediff = int((time2 - time1).total_seconds()) quotient, a['second'] = divmod(timediff, 60) if quotient < 60: a['minute'] = quotient else: quotient, a['minute'] = divmod(quotient, 60) if quotient < 24: a['hour'] = quotient else: a['day'], a['hour'] = divmod(quotient, 24) for k, v in a.items(): if v == 1: result = str(v) + " " + k + " " + result if v > 1: result = str(v) + " " + k + "s " + result print(line.strip() + " --- " + result)