1

Possible Duplicate:
IndentationError: unindent does not match any outer indentation level

I have the following python code.

import sys

ins = open( sys.argv[1], "r" )
array = []
for line in ins:
    s = line.split()
    array.append( s[0] ) # <-- Error here 
print array

ins.close()

The python interpreter complains

  File "sort.py", line 7
    array.append( s[0] )
                       ^
IndentationError: unindent does not match any outer indentation level

Why so? And how to correct this error?

Community
  • 1
  • 1
pythonic
  • 20,589
  • 43
  • 136
  • 219

3 Answers3

4

You are mixing tabs and spaces (happens sometimes :). Use one or the other.

I looked at your source:

    s = line.split()  # there's a tab at the start of the line
    array.append( s[0] )  # spaces at the start of the line

Aside: As just a friendly suggestion, consider using with to open your file. The advantage is that the file will be automatically closed for you (no close() needed) when you are done or an exception is encountered.

array = []
with open( sys.argv[1], "r" ) as ins:  # "r" really not needed, it's the default.
   for line in ins:
      s = line.split()
      # etc...
Levon
  • 138,105
  • 33
  • 200
  • 191
3

run your code with python -tt sort.py.

It'll show you whether you've mixed tabs and spaces or not.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
2

Make sure your indentation is consistent using either spaces or tabs and not a mix of the two.

Christian Witts
  • 11,375
  • 1
  • 33
  • 46