The code is:
from datetime import datetime,time
from csv import reader
with open('onlyOnce.txt', 'r+') as fonlyOnce:
for f_time, sec_time, dte in filter(None, reader(fonlyOnce, delimiter="_")):
check_stime=f_time.split(":")
Stask_hour=check_stime[0]
Stask_minutes=check_stime[1]
check_stime = datetime.strptime(f_time,"%H:%m").time()
check_etime=sec_time.split(":")
Etask_hour=check_etime[0]
Etask_minutes=check_etime[1]
#check every minute if current information = desired information
now = datetime.now()
now_time = now.time()
date_now = now.date()
if (date_now.strftime("%Y-%m-%d") == dte and time(int(Stask_hour),int(Stask_minutes)) <= now_time <= time(int(Etask_hour),int(Etask_minutes))):
print("this line in range time: "+ f_time)
#delete this line
fonlyOnce.write(" ")
else:
print("Padraic Cunningham")
fonlyOnce.close()
The goal of this code is to :
1- loop on the lines in the file
2- check if any line it in the range of current time
3- if yes: print this line in range time: 9:1
and delete this line from the same file.
4-data in the file is:
7:1_8:35_2016-04-14
8:1_9:35_2016-04-14
9:1_10:35_2016-04-14
5- output must be:
7:1_8:35_2016-04-14
8:1_9:35_2016-04-14
because the last line has the time in range of current time.it must delete and replace empty line.
My problem is this code will clean all file and i don't want that:
invaild code: fonlyOnce.write(" ")
Thanks