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


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

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

Started byGanesh Pal <ganesh1pal@gmail.com>
First post2016-02-27 22:20 +0530
Last post2016-02-29 00:00 +1100
Articles 2 — 2 participants

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


Contents

  list index out of range Error , need to fix it or ignore it Ganesh Pal <ganesh1pal@gmail.com> - 2016-02-27 22:20 +0530
    Re: list index out of range Error , need to fix it or ignore it Steven D'Aprano <steve@pearwood.info> - 2016-02-29 00:00 +1100

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

FromGanesh Pal <ganesh1pal@gmail.com>
Date2016-02-27 22:20 +0530
Subjectlist index out of range Error , need to fix it or ignore it
Message-ID<mailman.181.1456591812.20994.python-list@python.org>
Iam on python 2.6 and Linux , I need  input on the below program ,
here is the spinet of my program


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')
    print(lines)
    for line in lines:
        if 'exited' in lines:
            continue

        file = lines[0].split()[9][6:]
        break
    print file
    return file

def main():
    functions = [check_file]
    for func in functions:
        try:
            func()
        except Exception as e:
            return False
if __name__ == '__main__':
      main()



1.If the file is present in any one of the machine the program works
fine , example if the file is in machine4 it works fine,


machine-4# touch /tmp2/2.txt
machine-4# python c_4.py
run_al_parall 'ls -al /tmp2/2.txt'
machine-4: -rw-r--r-- 1 root  wheel  0 Feb 27 08:15 /tmp2/2.txt
gpal-machine-2 exited with status 1
gpal-machine-5 exited with status 1
gpal-machine-3 exited with status 1
gpal-machine-1 exited with status 1

['machine-4: -rw-r--r-- 1 root  wheel  0 Feb 27 08:15 /tmp2/2.txt',
'gpal-machine-2 exited with status 1', 'gpal-machine-5 exited with
status 1', 'gpal-machine-3 exited with status 1', 'gpal-machine-1
exited with status 1']
2.txt


2. But if the file is not present we get index out of range error , do
we need to fix this or its expected ? or its ok.

machine-4# python c_4.py
isi_for_array 'ls -al /tmp2/2.txt'
machine-2 exited with status 1
machine-1 exited with status 1
machine-4 exited with status 1
machine-5 exited with status 1
machine-3 exited with status 1

['machine-2 exited with status 1', 'machine-1 exited with status 1',
'machine-4 exited with status 1', 'machine-5 exited with status 1',
'machine-3 exited with status 1']
ERROR:root:list index out of range

3. Any other tips to improve the program

Regards,
Ganesh

[toc] | [next] | [standalone]


#103631

FromSteven D'Aprano <steve@pearwood.info>
Date2016-02-29 00:00 +1100
Message-ID<56d2ef7e$0$1584$c3e8da3$5496439d@news.astraweb.com>
In reply to#103592
On Sun, 28 Feb 2016 03:50 am, Ganesh Pal wrote:

> Iam on python 2.6 and Linux , I need  input on the below program ,
> here is the spinet of my program
> 
> 
> 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

Since filename is never assigned to inside the function, you don't need to
declare it global.

But better is to make the filename an argument of the function. That will
allow you to check for different files, not always the same one.


>     baddr = ''

Not used. Get rid of it.


>     cmd = ("run_al_paral 'ls -al %s'" % (filename))
>     print(cmd)
>     stdout, stderr, exitcode = run(cmd)

run is not defined.


>     print(stdout)
>     lines = stdout.strip().split('\n')
>     print(lines)
>     for line in lines:
>         if 'exited' in lines:
>             continue
>         file = lines[0].split()[9][6:]
>         break
>     print file
>     return file
> 
> def main():
>     functions = [check_file]
>     for func in functions:
>         try:
>             func()
>         except Exception as e:
>             return False

Why do you bother to return False? Nothing checks the return value of
main().

As far as the try...except block, please read this:

https://realpython.com/blog/python/the-most-diabolical-python-antipattern/



> if __name__ == '__main__':
>       main()
> 
> 
> 
> 1.If the file is present in any one of the machine the program works
> fine , example if the file is in machine4 it works fine,
> 
> 
> machine-4# touch /tmp2/2.txt
> machine-4# python c_4.py
> run_al_parall 'ls -al /tmp2/2.txt'
> machine-4: -rw-r--r-- 1 root  wheel  0 Feb 27 08:15 /tmp2/2.txt
> gpal-machine-2 exited with status 1
> gpal-machine-5 exited with status 1
> gpal-machine-3 exited with status 1
> gpal-machine-1 exited with status 1

You really should get out of the bad habit of running code as root. Once day
you will accidentally have a bug in your code that will do something it
shouldn't do (like delete the root file system) and leave your machine
unusable.

You should create a user with the minimum privileges needed to get the work
done. At the very least, you should TEST your code while running as an
unprivileged user. It is very dangerous to run buggy code as root. Who
knows what it will do?


> 2. But if the file is not present we get index out of range error , do
> we need to fix this or its expected ? or its ok.

Of course you need to fix it.

There's no way of telling where that error occurs or why it is being
printed.




-- 
Steven

[toc] | [prev] | [standalone]


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


csiph-web