IndentationError: expected an indented block but it's there
Peter Otten
__peter__ at web.de
Tue May 28 12:19:47 EDT 2013
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.
More information about the Python-list
mailing list