sending an argument

Mark McEahern marklists at mceahern.com
Sat Aug 3 00:36:41 CEST 2002


>      Could anybody please tell me on how to send an argument to the 
> python program when starting the program like we send argument to c++ 
> executable.  How to read the input entered along with the .py executable 
>  (like "server.py 8000")

Short answer:  sys.argv.

Long answer:

#!/usr/bin/env python
# foo.py

if __name__ == "__main__":
  import sys
  import os
  usage = "%s count" % (os.path.basename(sys.argv[0]))
  try:
    count = int(sys.argv[1])
    print count ** 2
  except:
    print usage
    sys.exit(1)

// m
-





More information about the Python-list mailing list