I'm writing a code in Python on Spyder, using Tkinter. You can see one of my methods below. Didn't want to put my full code because it's pretty long. I need to open a file to read some information. There is not a problem in my code and it actually works pretty well (does what i want it to do) but I couldn't figure out how to close my file after I'm done with it. Wherever i put my 'myFile.close()' command it gives me another type of error. I want to learn about where should i put it. I'm a little confused about where to put it because of for and if's. Would you please help me?
*** EDITED ***
With the myFile1.close() commands below it gives an "ValueError: I/O operation on closed file." error.
- I didn't use myFile1 in another place.
- There isn't another myFile1.close() command.
def login(self):
id_val = self.entry_1.get()
pass_val = self.entry_2.get()
myFile1 = open("ids.txt", "r")
for line in myFile1:
if line.find(id_val) == 0:
self.pid,password,name,gender,age,phone=line.split(",")
if password == pass_val:
box = Text(width=45, height=40)
box.place(x=1120, y=80)
self.first,self.last=name.split()
box.insert(END, "
Ad: ")
box.insert(INSERT, self.first)
box.insert(END, "
Soyad: ")
box.insert(INSERT, self.last)
box.insert(END, "
Cinsiyet: ")
box.insert(INSERT, gender)
box.insert(END, "
Ya?: ")
box.insert(INSERT, age)
box.insert(END, "
Telefon: ")
box.insert(INSERT, phone)
self.patient_screen()
myFile1.close()
else:
messagebox.showerror("Warning", "Wrong ID or password.")
myFile1.close()
break
else:
messagebox.showerror("Warning", "Wrong ID or password.")
myFile1.close()
break
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…