I am learing the book: Foundations of Python Network Programming - Apress 2004

When I tried this:

import socket, sys
 
host = sys.argv[1]
textport =  sys.argv[2]
 
#....

Then my IDE notify me this exception when debug:
"""IndexError: list index out of range:
host = sys.argv[1] """

When I tried to change number 1 and 2 above to 0 and 1 then it notify again at number 1 but not 0 anymore.:sad:

I guess that is a change in Python 2.4?!:?:
Please tell me why? Thks.

Recommended Answers

All 4 Replies

Here are the basics of commandline arguments ...

# commandline arguments

import sys 

print sys.argv[0] # prints the filename of your script 
print sys.argv[1] # the first parameter
print sys.argv[2] # the second parameter
#or 
print sys.argv[1:] # all parameters

sys.arg[1] and higher is only present if you give enough arguments in your command line, for instance "myprogram.py arg1 arg2 arg3" would assign data to sys.argv[0] through sys.argv[3]. If you try to use sys.argv[4], it will give you the error you have experienced.

Thank you very much for your reply. That really helps me so much.

I now can see that when running my program from my command line, it must take into it enough params.

But when debugging using an IDE what would I do to pass those params? Or I must for get about using the sys.argv[...] command? Could you please help me with this technique?

Thank you

That's great
Thank you very much indeed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.