Indentation as an indication of programming logic is one of Python’s most well-known features. But it’s often one of the more difficult aspects of the language for people to get used to when they first start writing Python code. The error messages associated with indentation issues can also be confusing. For example, the “indentationerror: expected an indented block after ‘with’ statement” can be baffling at first glance. But it’s easy to fix that error by taking a look into why it comes up in the first place.
Indentation and With Statements
With statements, like most elements in Python, use whitespace to signify program logic. The error is essentially telling you that the line of code following a with statement should be indented but isn’t. When you see this error, it typically just means that you need to indent the line following the error message. But it’s also important to understand why this error occurs in the first place.
A Closer Look at Indentation Errors
With statements are essentially coding shorthand that can make resource handling in Python more elegant. However, a with statement still needs to stay in line with Python’s underlying syntactical rules. A with statement essentially combines multiple elements in Python into a singular whole. But those elements, now joined, operate like any other part of Python. That means that a with statement that begins programming logic will need to use indentation in the same way as any conditional. Consider the following example.
with open(“testforexample.txt”, “w”) as ourFile:
ourFile.write(“Is there an error?”)
This code opens a file called testforexample.txt and assigns it to a variable called ourFile. We pass a w flag to open in order to tell Python that we want to use write mode. In line 2 we try to actually write some text into the newly opened file. However, when we run the code it just generates a Python error. The error stems from the fact that we haven’t indented line 2. Take special note of the colon used to terminate line 1. When you see a colon terminating one line it generally means we need to indent the following line. You can think of it as an indicator that a new code block has begun and will continue on in the following lines.
How To Fix the Error
Actually fixing this error is quite simple. You just need to indent the line or lines following the error. For example, in the earlier code the error statement pointed to line 1. This indicates that the following line, line 2, needs indentation. Try adding four spaces at the beginning of line 2 so that it looks like this.
with open(“testforexample.txt”, “w”) as ourFile:
ourFile.write(“Is there an error?”)
If you run this code it should function as expected with no error message. If you look in the directory that you ran it from, you should also see a text file. And the question “Is there an error?” should be written within it.
This is a fairly simple example of how to fix a with-based indentation error. But the same problem-solving approach will work on code of any size or complexity. You simply need to follow the error message to a problematic with statement and correct the intention beneath it.