-3

part of script

if sys.argv[1]  == 'help':
    help()
elif len(sys.argv) < 5:
    use()
else:
    pass


host = sys.argv[1]
username = sys.argv[2]
password = sys.argv[3]
node = sys.argv[4]
opts = sys.argv[5]

this is just part of code where problem is occuring.. when i run it , error occured :

host = sys.argv[1]

IndexError: list index out of range

All were working well, but i just reinstalled python. But, now it is creating problem. This is well working in linux still.

" Through some search, i came to know that '.py association in the registry is incorrect. It's missing %* at the end.' but, i don't know how to fix it.

Current setup path in environment variable is C:\Python27;C:\Python27\Lib\site-packages\;C: \Python27\Scripts\

4

1 回答 1

3

sys.argv是一个和其他列表一样的列表,除了它是从命令行创建的。

它具有与您用于运行脚本的命令行所指示的一样多的项目。

代码从来都不是正确的,问题与您的 Python 文件关联或路径无关。(如果是这样,您会从命令行收到错误,但您会收到来自 Python 的错误。)

问题是您假设会有sys.argv[1], 通过'help'在对 的长度进行任何检查之前进行检查sys.argv。如果脚本在没有任何参数的情况下运行,则该[1]索引超出范围。

于 2013-04-13T09:50:02.890 回答