我在使用 timetracker 程序时遇到问题,我试图通过对其进行迭代来识别文件中的一行,然后编写这些行,除非其中有任何带有变量“delete”的东西,出于某种原因,它会通过文件并说它已被删除,但循环不会删除任何行。
date = input(" What is today's date? month-day-year format please. (E.G. 01-14-2003) ")
if os.path.exists(date):
today = open(date, "r")
print(today.read())
delete = input(" Which appointment would you like to delete? (Please type the time E.G. 7:00) ")
#Open the file, save the read to a variable, iterate over the file, check to see if the time is what user entered, if it is not then write it to the line, close the file.
fileEdit = open(date, "r+")
for line in today.readline():
print(line)
if delete not in line:
fileEdit.write(line)
print(fileEdit.read())
today.close()
fileEdit.close()
print ("Appointment deleted, goodbye")