Python quit() 函数 returns 错误。我正在使用 spyder 4.1.2
Python quit() function returns error. I am using spyder 4.1.2
file = input("Enter file name:")
try:
fhand = open(file,'r')
except:
print("File not found")
quit() # Error: name 'quit' is not defined
count = 0
for line in fhand:
line =line.strip()
if line.startswith('Subject'):
count+=1
print('There were,',count,'subject lines in ',file)
不应出现此错误。我很困惑我在这里做错了什么。我收到错误 “名称 'quit' 未定义”。哪个不应该来。
quit()
和 exit()
依赖于 site
模块,据我所知,它们被设计用于交互模式而不是实际程序或生产代码.
相反,我建议让您的程序看起来更像这样:
file = input("Enter file name:")
try:
fhand = open(file,'r')
count = 0
for line in fhand:
line =line.strip()
if line.startswith('Subject'):
count+=1
fhand.close()
print('There were,',count,'subject lines in ',file)
except FileNotFoundError:
print("File not found")
您可能还想立即阅读文件并尽快关闭它。
file = input("Enter file name:")
try:
fhand = open(file,'r')
except:
print("File not found")
quit() # Error: name 'quit' is not defined
count = 0
for line in fhand:
line =line.strip()
if line.startswith('Subject'):
count+=1
print('There were,',count,'subject lines in ',file)
不应出现此错误。我很困惑我在这里做错了什么。我收到错误 “名称 'quit' 未定义”。哪个不应该来。
quit()
和 exit()
依赖于 site
模块,据我所知,它们被设计用于交互模式而不是实际程序或生产代码.
相反,我建议让您的程序看起来更像这样:
file = input("Enter file name:")
try:
fhand = open(file,'r')
count = 0
for line in fhand:
line =line.strip()
if line.startswith('Subject'):
count+=1
fhand.close()
print('There were,',count,'subject lines in ',file)
except FileNotFoundError:
print("File not found")
您可能还想立即阅读文件并尽快关闭它。