Python sys.argv lists and indexes

Posted by Fred Gerbig on Stack Overflow See other posts from Stack Overflow or by Fred Gerbig
Published on 2010-04-12T23:05:25Z Indexed on 2010/04/12 23:12 UTC
Read the original article Hit count: 300

Filed under:

In the below code I understand that sys.argv uses lists, however I am not clear on how the index's are used here.

def main():

  if len(sys.argv) >= 2:
    name = sys.argv[1]
  else:
    name = 'World'
  print 'Hello', name

if __name__ == '__main__':
  main()

If I change

name = sys.argv[1]

to

name = sys.argv[0] 

and type something for an argument it returns:

Hello C:\Documents and Settings\fred\My Documents\Downloads\google-python-exercises
\google-python-exercises\hello.py

Which kind of make sense.

Can someone explain how the 2 is used here:

if len(sys.argv) >= 2:

And how the 1 is used here:

name = sys.argv[1] 

© Stack Overflow or respective owner

Related posts about python