Search code examples
pythonindentation

Python IndentationError: unexpected indent


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

Solution

  • 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!