2016-04-16 93 views
0

試圖創建一個文件來打開現有的文本文件並在該文件中查找行號。如果行號不在那裏,我希望它打印出一條消息,說它不在那裏。 這是我迄今爲止。我收到意外的EOF錯誤信息。我在哪裏錯過了這個問題?在解析時獲取意外的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

這是可以預料的。您的第一個嘗試塊沒有除外塊。

try: 
    # Your code goes here 
except: 
    print("Error") 
+0

啊,你應該澄清,這是事實,不具備除固定塊 – Natecat

+0

@Natecat匹配的第一個try語句。 – intboolstring