用Python关闭文件



我正在Spyder上用Python编写一段代码,使用Tkinter。你可以在下面看到我的一个方法。我不想放我的完整代码,因为它很长。我需要打开一个文件来阅读一些信息。我的代码中没有问题,它实际上运行得很好(做了我想做的事(,但我不知道如何在完成后关闭我的文件。无论我把"myFile.close(("命令放在哪里,它都会给我另一种类型的错误。我想知道我应该把它放在哪里。我有点困惑,因为for和if。你能帮帮我吗?

***编辑***通过下面的myFile1.close((命令;ValueError:对关闭的文件执行I/O操作"错误

  • 我没有在其他地方使用myFile1
  • 没有其他myFile1.close((命令
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, "nntAd: ")
box.insert(INSERT, self.first)
box.insert(END, "nntSoyad: ")
box.insert(INSERT, self.last)        
box.insert(END, "nntCinsiyet: ")
box.insert(INSERT, gender)
box.insert(END, "nntYaş: ")
box.insert(INSERT, age)
box.insert(END, "nntTelefon: ")
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

这是打开和关闭文件的正确但糟糕的方法:

f = open(filename,...)
# do something with f
f.close()

它有效,但也存在问题。如果您想在中间使用return,或者break,或者出现异常,该怎么办?然后你必须这样做:

f = open(filename, ...)
# do something with f
if condition:
f.close()
return something
# do something else with f
try:
# do something which may fail
except:
f.close()
raise
if another condition:
f.close()
return something_else
# do more  with f
f.close()

这很快就会变得丑陋。为了避免这种情况,最好使用上下文管理器协议来打开使用with关键字的文件,因为这些文件会自动关闭。

同样复杂的例子:

with open(filename, ...) as f:
# do something with f
if condition:
return something
# do something else with f
# do something which may fail
if another condition:
return something_else
# do more  with f
# here, the file is closed automatically

我不熟悉Spyder或Tkinter,但我建议对文件使用上下文管理器,它可以确保在上下文管理器之后关闭资源。

def login(self):
id_val = self.entry_1.get()
pass_val = self.entry_2.get()
# myFile1 = open("ids.txt", "r")
with open("ids.txt", "r") as myFile1 :
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, "nntAd: ")
box.insert(INSERT, self.first)
box.insert(END, "nntSoyad: ")
box.insert(INSERT, self.last)        
box.insert(END, "nntCinsiyet: ")
box.insert(INSERT, gender)
box.insert(END, "nntYaş: ")
box.insert(INSERT, age)
box.insert(END, "nntTelefon: ")
box.insert(INSERT, phone)
self.patient_screen()
else:
messagebox.showerror("Warning", "Wrong ID or password.")
break
else:
messagebox.showerror("Warning", "Wrong ID or password.")
break

基于Caleb的答案:为了可读性,我强烈建议先检查错误,使代码变平,这样你就可以快速脱离循环,然后继续进行相同级别的缩进,因为你知道你不再处于错误状态。例如:

def login(self):
id_val = self.entry_1.get()
pass_val = self.entry_2.get()
with open("ids.txt", "r") as myFile1 :
for line in myFile1:
if line.find(id_val) != 0:
messagebox.showerror("Warning", "Wrong ID or password.")
break
# If you've reached this line, you know the ID is correct.
self.pid,password,name,gender,age,phone=line.split(",")
if password != pass_val:
messagebox.showerror("Warning", "Wrong ID or password.")
break
# If you've reached this line, you know the password is correct.
box = Text(width=45, height=40)
box.place(x=1120, y=80)
self.first,self.last=name.split()
box.insert(END, "nntAd: ")
box.insert(INSERT, self.first)
box.insert(END, "nntSoyad: ")
box.insert(INSERT, self.last)        
box.insert(END, "nntCinsiyet: ")
box.insert(INSERT, gender)
box.insert(END, "nntYaş: ")
box.insert(INSERT, age)
box.insert(END, "nntTelefon: ")
box.insert(INSERT, phone)
self.patient_screen()

如果没有一大堆嵌套的if语句,它的可读性会高得多。

相关内容

  • 没有找到相关文章

最新更新