Search code examples
pythontry-catchexcept

Try / Except indentation error


I have been creating a program that sorts files and I need a try to catch an error

here is the code:

    try: 
        shutil.move(path, directory)
    except OSError:
        print 'ERROR: File name already in use'
        quit()

but when I try this the error:

    except OSError:
    ^
IndentationError: unexpected unindent

Any suggestion would be appreciated


Solution

  • Pasting your code into a text editor that shows whitespace, I see you mix tabs and spaces.

    enter image description here

    This is generally a bad idea - although a tab character may look like four spaces in your editor and on Stack Overflow, Python actually interprets it as a variable number of spaces between 1 and 8! (See Lexical Analysis - Indentation for more detail.)

    Rewrite your code so it uses only spaces.

        try: 
            shutil.move(path, directory)
        except OSError:
            print 'ERROR: File name already in use'
            quit()