Python 在哪里安装quit()函数?
这是输入:Python 在哪里安装quit()函数?,python,Python,这是输入: inp = input("Enter the file name: ") count = 0 try: x = open(inp) except: print("This won't work") quit() for line in x: count += 1 print("There are", count, "lines in", inp) 这是输出 NameER
inp = input("Enter the file name: ")
count = 0
try:
x = open(inp)
except:
print("This won't work")
quit()
for line in x:
count += 1
print("There are", count, "lines in", inp)
这是输出
NameERROR: name 'quit' is not defined
quit()
,exit()
,os.\u exit()
,sys.exit()。我想它不在库中,在哪里可以下载包含quit()
函数的模块?我相信您正在寻找exit()
输出:
0
1.
2.
3.
4.
5
“退出()不起作用”
检查except块是否正在执行您可能需要了解更多有关try except else finally
语法的信息。你可以。在这里获取更多关于这方面的信息
我还建议您通读python中的文件打开选项。链接到这里。我会使用with选项。我不会改变你的代码来反映所有的新变化,因为你仍然在探索python的奇妙世界
同时,以下是一些解决当前问题的方法
inp = input("Enter the file name: ")
count = 0
try:
x = open(inp) #this will open in read text mode
except:
print("This won't work")
else: #refer to https://stackoverflow.com/questions/855759/python-try-else
for line in x:
count += 1
print("There are", count, "lines in", inp)
或者你也可以试着这样做
inp = input("Enter the file name: ")
count = 0
success = True
try:
x = open(inp) #this will open in read text mode
except:
print("This won't work")
success = False
if success:
for line in x:
count += 1
print("There are", count, "lines in", inp)
要使用os
或sys
您首先需要导入它们。您的代码适合我<代码>退出()和退出()是内置的。如果要使用sys.exit()
必须首先导入sys
。没有os.exit()
。在发布问题之前,请在堆栈溢出中搜索类似错误。有类似的问题和答案。检查此链接[
inp = input("Enter the file name: ")
count = 0
success = True
try:
x = open(inp) #this will open in read text mode
except:
print("This won't work")
success = False
if success:
for line in x:
count += 1
print("There are", count, "lines in", inp)