简体   繁体   中英

Loop IndexError: list index out of range

I have this error

onecheck(sys.argv[1],sys.argv[2],sys.argv[3]) 
IndexError: list index out of range

I try to make loop a python script .

This is code :

with open(file) as k:
     for line in k:
         aa, bb, cc = line.split(':')
         time.sleep(5)
         os.system("python checkfile.py " + cc + " " + aa + " " + bb)

Last line from file is working

Why don't import checkfile directly, don't run via os.system()

If you are looking for a non-blocking way to execute your function in checkfile.py , look at :

:

import checkfile


with open(file) as k:
    for line in k:
        ltab = line.split(':')
        if len(ltab) > 2:
            time.sleep(5)
            checkfile.my_func(ltab[0], ltab[1], ltab[2])

Issue : You're getting any of the aa , bb , cc empty at:

aa, bb, cc = line.split(':')

Suggestion : In addition to above comments, you can also put your call in length check:

if len(sys.argv)== 4:  # index 0 - 3 
    onecheck(sys.argv[1],sys.argv[2],sys.argv[3]) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM