Unexpected EOF while parsing

Unexpected EOF( End Of File ) while parsing is a syntax error which means the end of source code is reached even before all the blocks of code are completed. This happens in a number of situations in Python, such as:

  1. Missing or unmatched parentheses.
  2. Forget to enclose code inside a special statement.
  3. Unfinished try statement.

Missing or unmatched parentheses

This can simply also mean you are missing a closing parenthesis or curly bracket in a block of code or have too many parentheses.

print("This is Python";

The above statement will result in unexpected EOF because the statement misses the closing parentheses ")". You can correct it by add the closing parentheses.

print("This is Python");

In order to avoid the above situations, you should keep your code clean and concise, reducing the number of operations occurring on one line where suitable.

Forget to enclose code inside a special statement

In this case, Python can't find an indented block of code to pair with your statement.

for i in range(50):

In the above statement, a code block starts with a statement like for i in range(50): and requires at least one line afterwards that contains code that should be in it. So, the above statement will result in unexpected EOF because the statement forget to enclose code inside for loop statement.

To avoid this error, you have to enter the whole code block as a single input:

for i in range(50): print(i, end=', ')

Unfinished try statement

This is another situation that produced this exception: if you write a try block without any except or finally will result in unexpected EOF.

try: print('Inside try block')

In the above statement, a try block without any except or finally clause. So this will result in unexpected EOF because Python is expecting at least one except or finally clause.

To avoid this error, you should write except or finally clause with your try statement.

try: print('Inside try block') except: print('Inside except block')

Or

try: print('Inside try block') finally: print('Inside finally block')

What is EOF in Python?


Python unexpected EOF

EOF stands for "End Of File", is a special delimiter or data that is placed at the end of a file after the last byte of data in the file. This represents the last character in a Python program.