How to pass command line arguments to your Python program
This is my OLD blog. I've copied this post over to my NEW blog at:
http://www.saltycrane.com/blog/2007/12/how-to-pass-command-line-arguments-to/
You should be redirected in 2 seconds.
Here is an example for quick reference. argv
holds the program
name at index 0. That's why we start at 1.
#!/usr/bin/python import sys def main(): # print command line arguments for arg in sys.argv[1:]: print arg if __name__ == "__main__": main()
Try it out:
$ python cmdline_args.py arg1 arg2 arg3 arg1 arg2 arg3
See also:
sys
module documentationgetopt
module documentationGuido van Rossum's post
No comments:
Post a Comment