Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

indentation error

7 views
Skip to first unread message

asit

unread,
Mar 5, 2010, 3:04:13 AM3/5/10
to
Consider the following code

import stat, sys, os, string, commands

try:
pattern = raw_input("Enter the file pattern to search for :\n")
commandString = "find " + pattern
commandOutput = commands.getoutput(commandString)
findResults = string.split(commandOutput, "\n")
print "Files : "
print commandOutput
print "============================="
for file in findResults:
mode = stat.S_IMODE(os.lstat(file)[stat.ST_MODE])
print "\nPermissions for file", file, ":"
for level in "USR", "GRP", "OTH":
for perm in "R", "W", "X":
if mode & getattr(stat,"S_I"+perm+level):
print level, " has ", perm, " permission"
else:
print level, " does NOT have ", perm, "
permission"
except:
print "There was a problem - check the message above"


According to me, indentation is ok. but the python interpreter gives
an indentation error

[asit ~/py] $ python search.py
File "search.py", line 7
findResults = string.split(commandOutput, "\n")
^
IndentationError: unindent does not match any outer indentation level

Ulrich Eckhardt

unread,
Mar 5, 2010, 3:32:17 AM3/5/10
to
asit wrote:
> pattern = raw_input("Enter the file pattern to search for :\n")
> commandString = "find " + pattern
> commandOutput = commands.getoutput(commandString)
> findResults = string.split(commandOutput, "\n")
> print "Files : "
> print commandOutput
> print "============================="
> for file in findResults:
> mode = stat.S_IMODE(os.lstat(file)[stat.ST_MODE])
> print "\nPermissions for file", file, ":"
> for level in "USR", "GRP", "OTH":
> for perm in "R", "W", "X":
> if mode & getattr(stat,"S_I"+perm+level):
> print level, " has ", perm, " permission"
> else:
> print level, " does NOT have ", perm, "
> permission"
[...]

> According to me, indentation is ok. but the python interpreter gives
> an indentation error
>
> [asit ~/py] $ python search.py
> File "search.py", line 7
> findResults = string.split(commandOutput, "\n")
> ^
> IndentationError: unindent does not match any outer indentation level

Hard to tell, since your posting was already line-broken which already
changes the correctness. In any case, watch for tabs/spaces, many editors
have an option to display whitespace.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

Steven D'Aprano

unread,
Mar 5, 2010, 6:48:35 AM3/5/10
to
On Fri, 05 Mar 2010 00:04:13 -0800, asit wrote:

> Consider the following code
[snip]


> According to me, indentation is ok. but the python interpreter gives an
> indentation error

You can trust the interpreter. There *is* an indentation error. Most
likely you have mixed spaces and tabs. Try:

python -t -t search.py

instead. (Yes, -t twice.)

On an unrelated note, your code snippet shows a very poor coding style.
Firstly, bare "except" clauses are VERY bad practice: it will mask bugs
in your code. Secondly, you should wrap the smallest amount of code in
the try block as you need.

Something like this is probably better:


pattern = raw_input("Enter the file pattern to search for :\n")
commandString = "find " + pattern
commandOutput = commands.getoutput(commandString)
findResults = string.split(commandOutput, "\n")
print "Files : "
print commandOutput
print "============================="
for file in findResults:

try:


mode = stat.S_IMODE(os.lstat(file)[stat.ST_MODE])

except (IOError, OSError):
# Print a useless error message so your users will hate you.
print "There was a problem with %s" % file
continue


print "\nPermissions for file", file, ":"
for level in "USR", "GRP", "OTH":
for perm in "R", "W", "X":
if mode & getattr(stat,"S_I"+perm+level):
print level, " has ", perm, " permission"
else:
print level, " does NOT have ", perm, "permission"

--
Steven

Tim Roberts

unread,
Mar 6, 2010, 7:49:33 PM3/6/10
to
asit <lip...@gmail.com> wrote:
>
>According to me, indentation is ok. but the python interpreter gives
>an indentation error
>
>[asit ~/py] $ python search.py
> File "search.py", line 7
> findResults = string.split(commandOutput, "\n")
> ^
>IndentationError: unindent does not match any outer indentation level

The most likely cause is mixing spaces with tabs.
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

0 new messages