list index out of range Error , need to fix it or ignore it

Oscar Benjamin oscar.j.benjamin at gmail.com
Sun Feb 28 14:28:04 EST 2016


On 27 February 2016 at 16:50, Ganesh Pal <ganesh1pal at gmail.com> wrote:
> Iam on python 2.6 and Linux , I need  input on the below program ,
> here is the spinet of my program

It would be much better if you presented a complete program here.
Otherwise the missing parts will confuse people. See:
http://sscce.org/

> filename='/tmp2/2.txt'
>
> def check_file():
>     """
>      Run the command parallel on all the machines , if there is a
> file named /tmp/file2.txt  extract file2.txt
>
>     """
>     global filename
>     baddr = ''
>     cmd = ("run_al_paral 'ls -al %s'" % (filename))
>     print(cmd)
>     stdout, stderr, exitcode = run(cmd)
>     print(stdout)
>     lines = stdout.strip().split('\n')

.splitlines() is more portable than .split('\n') as it handles '\r' as well.

>     print(lines)

There are a few problems with the code below:

>     for line in lines:
>         if 'exited' in lines:
>             continue
>
>         file = lines[0].split()[9][6:]

Okay so you're skipping lines that contain the text "exited" in order
to find the line you want. That's fine except that you then try to
access the line you've found as lines[0] rather than line (singular).
This means that although your loop continues when the line contains
"exited" you're still accessing the first element of lines (which may
be one of the ones that contains "exited") in any case. I think that
you meant line rather than lines[0] here.

You're assuming that lines has at least one element. Otherwise
lines[0] would give IndexError. This won't raise IndexError since the
line of code "file = ..." is only executed inside the body of the for
loop which means that if lines is empty then execution doesn't reach
this point. I don't think that that was your intention though as
really it should be line rather than lines[0].

You're then assuming that lines[0].split() (which should really be
line.split())) has at least 10 elements since you ask for
lines[0].split()[9]. This will raise IndexError if .split() returnd
fewer than 10 substrings. This is most likely the IndexError that you
get. Your incorrect use of line rather than lines[0] means you're
reading one of the lines that contains "exited" and so when you call
.split() on that it gives fewer than 10 elements and so the last index
in lines[0].split()[9] raises IndexError.

>         break
>     print file

Here you're assuming that lines contains at least one string that does
not contain the substring "exited". Otherwise this line will leas to a
NameError since the name file is not bound to anything unless the
"file=" line is executed.

>     return file
>
> def main():
>     functions = [check_file]
>     for func in functions:
>         try:
>             func()
>         except Exception as e:
>             return False

Here you've crippled Python's ability to give quite useful error
messages. Replace the above with:

def main():
    check_file()

and then Python will print a helpful error message. More than that if
you learn to use a debugger you can hook into the exact moment where
an *uncaught* error is raised. Since you catch the error that won't be
possible. The traceback that Python prints *if you don't catch the
error* contains useful information about the problem. Even if you
don't understand that information other people do so if you post it
here then you can get much better help. It shows the line at which the
error occurred and has more description than simply IndexError.

If you remove the try/except you can then use pdb. Just rerun your code as

$ python -m pdb myscript.py

Type "c" (and hit enter) to "continue". This will run your script
right up to the point of the uncaught exception and then hook you into
a debugger at the moment the exception was raised. You can then use
"l" to show the surrounding code. Use "p file" to show the value of
for example the file variable if that seems to be the problem.

--
Oscar



More information about the Python-list mailing list