Python Forum

Full Version: IndentationError: unexpected indent
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The following is the class that received the IndentationError: unexpected indent. I got the error when "from reader.reader import Reader". Which indent is not supposed to? Thanks.

 class Reader:
    def __init__(self, filename):
        self.filename = filename
        self.f = open(self.filename, 'rt')

    def close(self):
        self.f.close()

    def read(self):
        return self.f.read()
Error:
Traceback (most recent call last): File "...\Python Project\venv\Main.py", line 1, in <module> from reader.reader import Reader File "...\Python Project\venv\reader.py", line 1 class Reader: IndentationError: unexpected indent
Please post your code in python tags.
Copy and paste likely fixed the error because I have no problems. It could be a mix of tabs and spaces. All indentation should be 4 spaces. My editor sees your indentation as 3 spaces for the first level (def's) and 4 spaces for the second level. That is bad, but usually not a syntax error.
(May-01-2022, 10:44 PM)deanhystad Wrote: [ -> ]Copy and paste likely fixed the error because I have no problems. It could be a mix of tabs and spaces. All indentation should be 4 spaces. My editor sees your indentation as 3 spaces for the first level (def's) and 4 spaces for the second level. That is bad, but usually not a syntax error.

Thanks. It is working now.