views:

594

answers:

5

I'm getting an error when I try to run my script

Error:"IndentationError: unindent does not match any outer indentation"

Code snipet that throws the error:

def update():
    try:
        lines = open("vbvuln.txt", "r").readlines()
    except(IOError): 
         print "[-] Error: Check your phpvuln.txt path and permissions"
         print "[-] Update Failed\n" 
        sys.exit(1)
    try:

This is the actual line that occurs the error:

print "[-] Update Failed\n"
A: 

You have an empty try block with nothing underneath it. This is causing the error.

BTW, your sys.exit(1) is off-indent as well. In python, Indentation is important because this is how Python interpreter determines code blocks. So, you have to indent your code properly to get your code running.

Aamir
The last `try` block is most likely not empty, he just stopped copying his code there. If it is empty, the error would be "expected an indented block". And without an `except` block it wouldn't work anyways ("invalid syntax")
Blixt
+6  A: 

Put a space before sys.exit(1) or remove space before print "[-] Error: Check your phpvuln.txt path and permissions" and print "[-] Update Failed\n".

Daniyar
A: 

The indentation error happens where you have indented incorrectly. Which is clearly visible by the fact that you have different indentation in the line starting with "sys".

Lennart Regebro
+3  A: 

As others have mentioned, you need to make sure that each code block has the exact same indentation.

What they haven't mentioned is that the widely adopted convention is to always use exactly 4 spaces per indentation. In your code, the print statements are indented using 5 spaces (most likely by accident.) So do not add another space to sys.exit(1); remove the spaces from the print statements.

Revised code:

def update():
    try:
        lines = open("vbvuln.txt", "r").readlines()
    except (IOError): 
        print "[-] Error: Check your phpvuln.txt path and permissions"
        print "[-] Update Failed\n" 
        sys.exit(1)
Blixt
While I'm also a follower of PEP8 I wouldn't say that 4 spaces in Python is a standard, rather, its a convention. Using more or less spaces won't break Python as long as you are consistent.
Andre Miller
You're right, I changed my wording. I'd still give the developer that didn't use 4 spaces for indentation the evil eye though... =)
Blixt
Anything other than 4 goes against the zen.
Robbie
+1  A: 

A good way to maintain a standard for your indentations is to use the tab key instead of spacebar.

John