I am trying to determine the amount of time passed since my program was opened. I have written to a file using strftime, however, when I attempt to read the file, it presents it in a different time format. Using strptime returns the error: "time data %r does not match format %r". The goal is to use delta time to get the hour difference between the current time, and the time in the file. Any help is appreciated.
import time
from datetime import datetime
from datetime import timedelta
from time import strptime, strftime
current_time = datetime.now()
time_file = open("times.txt", 'w+')
def log_time():
time_file.write(str(current_time.strftime('%H')))
time_file.seek(0)
def previous_time():
prev_time = time_file.read()
return strptime(prev_time, "%H")
#the goal here is too subtract previous time (the one in the file) from the current time.
log_time()
#I need to get previous_time and current_time into the same format so that I can subtract them.