29

Here is my code... I am getting an indentation error, but I don't know why it occurs.

->

# loop
while d <= end_date:
    # print d.strftime("%Y%m%d")
    fecha = d.strftime("%Y%m%d")

    # Set URL
    url = 'http://www.wpemergencia.omie.es//datosPub/marginalpdbc/marginalpdbc_' + fecha + '.1'

    # Descargamos fichero
    response = urllib2.urlopen(url)

    # Abrimos fichero
    output = open(fname,'wb')

    # Escribimos fichero
    output.write(response.read())

    # Cerramos y guardamos fichero
    output.close()

    # fecha++
    d += delta
3
  • There is no IndentationError here. It must be elsewhere in your code, along with the definitions of d and end_date. Please provide an SSCCE. Commented Dec 26, 2011 at 10:09
  • 2
    Any half-decent editor has an option to somehow show spaces and tabs visually. Use that. Commented Dec 26, 2011 at 10:09
  • Changed your tabs to spaces, copying this should now work. Commented Dec 26, 2011 at 21:51

5 Answers 5

50

Run your program with

python -t script.py

This will warn you if you have mixed tabs and spaces.

On Unix-like systems, you can see where the tabs are by running

cat -A script.py

And you can automatically convert tabs to 4 spaces with the command

expand -t 4 script.py > fixed_script.py

PS. Be sure to use a programming editor (e.g., Emacs and Vim), not a word processor, when programming. You won't get this problem with a programming editor.

PPS. For Emacs users, M-x whitespace-mode will show the same information as cat -A from within an Emacs buffer!

Sign up to request clarification or add additional context in comments.

3 Comments

Not that unutbu is not complete in his/her answer, I had the same issue and upon using cat -A script.py you can immediately see the missing indentations. Even if you copy and paste and it may look like an indentation in your editor. It in fact is not, so be careful.
What would be the equivalent of cat -A script.py on MacOS?
@LarsBingchong: According to this man page, the equivalent should be cat -vet script.py.
9

Find all tabs and replaced by 4 spaces in Notepad++. It worked.

1 Comment

After year and a half since original question has been posted, you actually couldn't find anything new to suggest. Welcome to the SO and please, read previous answers before you post your own. Don't repeat answers that exist already.
8

Check if you mixed tabs and spaces. That is a frequent source of indentation errors.

Comments

3

You can't mix tab and spaces for indentation. The best practice is to convert all tabs to spaces.

How can we fix this? Well, just delete all the spaces/tabs before each line and convert them uniformly either to tabs or spaces, but don't mix them. The best solution: enable the option in your editor to convert any tabs to spaces automagically.

Also be aware that your actual problem may lie in the lines before this block, and Python throws the error here, because of a leading invalid indentation which doesn't match the following indentations!

Comments

0

Simply copy your script, and put it under """" your entire code """"...

Specify this line in a variable... Like,

a = """ your entire code """
print a.replace('    ','    ') # First 4 spaces tab, second four spaces from the space bar

print a.replace('Here please press the tab button. It will insert some space"," Here simply press the space bar four times")
# Here we are replacing tab space by four character space as per the PEP 8 style guide...

Now execute this code in Sublime Text using Ctrl + B. Now it will print the indented code in the console. That's it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.