Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #46296 > unrolled thread

IndentationError: expected an indented block but it's there

Started byJackM <notreal@earthlink.net>
First post2013-05-28 11:32 -0400
Last post2013-05-29 17:59 +1000
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  IndentationError: expected an indented block but it's there JackM <notreal@earthlink.net> - 2013-05-28 11:32 -0400
    Re: IndentationError: expected an indented block but it's there Michael Torrie <torriem@gmail.com> - 2013-05-28 09:55 -0600
    Re: IndentationError: expected an indented block but it's there Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-28 16:01 +0000
    Re: IndentationError: expected an indented block but it's there Peter Otten <__peter__@web.de> - 2013-05-28 18:19 +0200
    Re: IndentationError: expected an indented block but it's there Chris Angelico <rosuav@gmail.com> - 2013-05-29 02:31 +1000
    Re: IndentationError: expected an indented block but it's there Peter Otten <__peter__@web.de> - 2013-05-28 18:53 +0200
    Re: IndentationError: expected an indented block but it's there Chris Angelico <rosuav@gmail.com> - 2013-05-29 17:59 +1000

#46296 — IndentationError: expected an indented block but it's there

FromJackM <notreal@earthlink.net>
Date2013-05-28 11:32 -0400
SubjectIndentationError: expected an indented block but it's there
Message-ID<V54pt.161708$Zp1.55817@en-nntp-15.dc1.easynews.com>
Having a problem getting a py script to execute. Got this error:

File "/scripts/blockIPv4.py", line 19
     ip = line.split(';')[0]
      ^
IndentationError: expected an indented block


I'm perplexed because the code that the error refers to *is* indented:



with open('/var/www/html/mydomain.com/banlist.txt','r') as inFile:
     for line in inFile.readlines():
         ip = line.split(';')[0]
         output = os.popen( '/etc/sysconfig/iptables -A INPUT -s ' + ip 
+ ' -j REJECT' )
         logFile.write(ip+' - Has been blocked\n')


What am I missing here?




-- 
My email address on the header is a non-monitored spam catching account. 
I can be reached via http://www.wvnh.net/contact.htm

[toc] | [next] | [standalone]


#46300

FromMichael Torrie <torriem@gmail.com>
Date2013-05-28 09:55 -0600
Message-ID<mailman.2301.1369756559.3114.python-list@python.org>
In reply to#46296
On 05/28/2013 09:32 AM, JackM wrote:
> Having a problem getting a py script to execute. Got this error:
> 
> File "/scripts/blockIPv4.py", line 19
>      ip = line.split(';')[0]
>       ^
> IndentationError: expected an indented block
> I'm perplexed because the code that the error refers to *is* indented:
> with open('/var/www/html/mydomain.com/banlist.txt','r') as inFile:
>      for line in inFile.readlines():
>          ip = line.split(';')[0]
>          output = os.popen( '/etc/sysconfig/iptables -A INPUT -s ' + ip 
> + ' -j REJECT' )
>          logFile.write(ip+' - Has been blocked\n')
> 
> 
> What am I missing here?

Indentation has to be consistent.  likely the for line is indented with
spaces and the next line with a tab.  You can use tabs and spaces, but
you have to be consistent with how you use them.  IE if level 1 is
indented with spaces, then level 2 has to be indented with spaces up to
level 1 as well.  Hope that makes sense.


[toc] | [prev] | [next] | [standalone]


#46303

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-05-28 16:01 +0000
Message-ID<51a4d4ca$0$29966$c3e8da3$5496439d@news.astraweb.com>
In reply to#46296
On Tue, 28 May 2013 11:32:06 -0400, JackM wrote:

> Having a problem getting a py script to execute. Got this error:
> 
> File "/scripts/blockIPv4.py", line 19
>      ip = line.split(';')[0]
>       ^
> IndentationError: expected an indented block
> 
> 
> I'm perplexed because the code that the error refers to *is* indented:

Whenever you get perplexing indentation errors, suspect an inconsistency 
due to mixed tabs and spaces.

Tabs good. Spaces good. Tabs and spaces together, bad.

You can run the tabnanny module over your source code:

python -m tabnanny  <file-or-directory>


to convert indentation to all spaces.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#46306

FromPeter Otten <__peter__@web.de>
Date2013-05-28 18:19 +0200
Message-ID<mailman.2302.1369757998.3114.python-list@python.org>
In reply to#46296
JackM wrote:

> Having a problem getting a py script to execute. Got this error:
> 
> File "/scripts/blockIPv4.py", line 19
>      ip = line.split(';')[0]
>       ^
> IndentationError: expected an indented block
> 
> 
> I'm perplexed because the code that the error refers to *is* indented:
> 
> 
> 
> with open('/var/www/html/mydomain.com/banlist.txt','r') as inFile:
>      for line in inFile.readlines():
>          ip = line.split(';')[0]
>          output = os.popen( '/etc/sysconfig/iptables -A INPUT -s ' + ip
> + ' -j REJECT' )
>          logFile.write(ip+' - Has been blocked\n')
> 
> 
> What am I missing here?

If you are mixing tabs and spaces to indent your code and have your editor 
configured with a tab width other than eight your code may look correct when 
it isn't. A simulation in the interactive interpreter:

The actual file contents:

>>> s = "if 1:\n\tif 2:\n    \tprint 'hi'"

What you see:

>>> print s.expandtabs(4)
if 1:
    if 2:
        print 'hi'
>>> exec s.expandtabs(4)
hi

What Python "sees":

>>> print s.expandtabs(8)
if 1:
        if 2:
        print 'hi'
>>> exec s
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 3
    print 'hi'
        ^
IndentationError: expected an indented block

Solution: configure your editor to use four spaces for indentation.

[toc] | [prev] | [next] | [standalone]


#46308

FromChris Angelico <rosuav@gmail.com>
Date2013-05-29 02:31 +1000
Message-ID<mailman.2304.1369758710.3114.python-list@python.org>
In reply to#46296
On Wed, May 29, 2013 at 2:19 AM, Peter Otten <__peter__@web.de> wrote:
> Solution: configure your editor to use four spaces for indentation.

ITYM eight spaces. But the real solution is to not mix tabs and
spaces. Stick to one or the other and you're safe.

ChrisA

[toc] | [prev] | [next] | [standalone]


#46311

FromPeter Otten <__peter__@web.de>
Date2013-05-28 18:53 +0200
Message-ID<mailman.2306.1369760011.3114.python-list@python.org>
In reply to#46296
Chris Angelico wrote:

> On Wed, May 29, 2013 at 2:19 AM, Peter Otten <__peter__@web.de> wrote:
>> Solution: configure your editor to use four spaces for indentation.
> 
> ITYM eight spaces. 

I meant: one hit of the Tab key should add spaces up to the next multiple of 
four. Which implies

> But the real solution is to not mix tabs and
> spaces. Stick to one or the other and you're safe.

[toc] | [prev] | [next] | [standalone]


#46359

FromChris Angelico <rosuav@gmail.com>
Date2013-05-29 17:59 +1000
Message-ID<mailman.2344.1369814374.3114.python-list@python.org>
In reply to#46296
On Wed, May 29, 2013 at 2:53 AM, Peter Otten <__peter__@web.de> wrote:
> Chris Angelico wrote:
>
>> On Wed, May 29, 2013 at 2:19 AM, Peter Otten <__peter__@web.de> wrote:
>>> Solution: configure your editor to use four spaces for indentation.
>>
>> ITYM eight spaces.
>
> I meant: one hit of the Tab key should add spaces up to the next multiple of
> four. Which implies
>
>> But the real solution is to not mix tabs and
>> spaces. Stick to one or the other and you're safe.

Sure. If you configure your tab *key* to not insert a tab *character*,
then you're fine. Or alternatively, if you always use \t for
indentation, you can tweak the displayed width of it. (Or, as I do,
just let it be eight wide. On today's screens that's not much of a
problem.)

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web