尝试创建一个打开现有文本文件的文件,并在该文件中查找行号。如果没有行号,我希望它打印出一条消息,表明它不在那里。 这就是我到目前为止所拥有的。我收到了意外的EOF错误消息。我在哪里错过了这个问题?
# Get Input from user
file_name = input("Name of file to open please: ")
try:
in_file=open(file_name)
while True:
find_line = input("Which line number are you looking for? ")
try:
line_num=int(find_line)
line_count=1
for line_num in in_file:
if line_count== find_line:
print("Line number {} of the file {}, reads: {}".format(find_line,file_name,line_num))
break
line_count+=1
else:
print("Line number {} in file {} seems to be missing".format(find_line,file_name))
in_file.close()
in_file.open(file_name)
continue
break
except ValueError:
print("The Line Number you entered",find_line,"is not a correct line number")
in_file.close()
except IOError:
print ("Not sure how to break this to you, but the file your requested",file_str,"well, it's just not there")
print ("end of program")
答案 0 :(得分:0)
这是可以预料的。您的第一个 try块没有except块。
try:
# Your code goes here
except:
print("Error")