I recently opened another Q about different thing, and now I want to keep count down the time also when the program is not running, I think I know how to do this, maybe calculate how much time is remaining and then minus it with the current time, but I'm not sure, so this is why I'm asking here.
Is there a way to keep calculate the time also when the program is not running, for example, I'm counting down from 60 , closing the program, and then when I open it i will see 45 seconds (15 seconds).
I want to write the time into a file and then reading from it.
Current Program
import os
from time import sleep
class CountDown:
def __init__(self):
if not os.path.exists('last_time.txt'):
with open('last_time.txt', 'w') as f:
pass
self.write_time()
def countdown(self, time=60) -> None:
while time > 0:
print(time)
sleep(1)
time -= 1
f = open("last_time.txt", 'w')
f.write(str(time))
f.close()
def write_time(self) -> None:
with open('last_time.txt', 'r') as f:
if not os.stat('last_time.txt').st_size == 0: # Check if file is empty
self.countdown(int(f.read()))
else:
self.countdown()
if __name__ == '__main__':
CountDown()