2016-04-16 19 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ブロックにはexceptブロックがありません。

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

ああ、あなたはそれが実際には固定ブロック – Natecat

+0

@Natecat除いてマッチングを持たない最初のtry文であることを明確にすべきです。 – intboolstring

関連する問題