proffreda

Python command line arguments

Jul 14th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. Python's command line arguments and environment variables
  2.  
  3. python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
  4. Options and arguments (and corresponding environment variables):
  5. -b : issue warnings about str(bytes_instance), str(bytearray_instance)
  6. and comparing bytes/bytearray with str. (-bb: issue errors)
  7. -B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x
  8. -c cmd : program passed in as string (terminates option list)
  9. -d : debug output from parser; also PYTHONDEBUG=x
  10. -E : ignore PYTHON* environment variables (such as PYTHONPATH)
  11. -h : print this help message and exit (also --help)
  12. -i : inspect interactively after running script; forces a prompt even
  13. if stdin does not appear to be a terminal; also PYTHONINSPECT=x
  14. -I : isolate Python from the user's environment (implies -E and -s)
  15. -m mod : run library module as a script (terminates option list)
  16. -O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x
  17. -OO : remove doc-strings in addition to the -O optimizations
  18. -q : don't print version and copyright messages on interactive startup
  19. -s : don't add user site directory to sys.path; also PYTHONNOUSERSITE
  20. -S : don't imply 'import site' on initialization
  21. -u : unbuffered binary stdout and stderr, stdin always buffered;
  22. also PYTHONUNBUFFERED=x
  23. see man page for details on internal buffering relating to '-u'
  24. -v : verbose (trace import statements); also PYTHONVERBOSE=x
  25. can be supplied multiple times to increase verbosity
  26. -V : print the Python version number and exit (also --version)
  27. -W arg : warning control; arg is action:message:category:module:lineno
  28. also PYTHONWARNINGS=arg
  29. -x : skip first line of source, allowing use of non-Unix forms of #!cmd
  30. -X opt : set implementation-specific option
  31. file : program read from script file
  32. - : program read from stdin (default; interactive mode if a tty)
  33. arg ...: arguments passed to program in sys.argv[1:]
  34.  
  35. Other environment variables:
  36. PYTHONSTARTUP: file executed on interactive startup (no default)
  37. PYTHONPATH : ':'-separated list of directories prefixed to the
  38. default module search path. The result is sys.path.
  39. PYTHONHOME : alternate <prefix> directory (or <prefix>:<exec_prefix>).
  40. The default module search path uses <prefix>/pythonX.X.
  41. PYTHONCASEOK : ignore case in 'import' statements (Windows).
  42. PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.
  43. PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.
  44. PYTHONHASHSEED: if this variable is set to 'random', a random value is used
  45. to seed the hashes of str, bytes and datetime objects. It can also be
  46. set to an integer in the range [0,4294967295] to get hash values with a
  47. predictable seed.
  48.  
  49.  
  50.  
  51. The Python sys module provides access to any command-line arguments via the sys.argv. This serves two purposes −
  52. sys.argv is the list of command-line arguments.
  53. len(sys.argv) is the number of command-line arguments.
  54.  
  55. Here sys.argv[0] is the program ie. script name.
  56.  
  57. Example
  58. Consider the following script test.py −
  59.  
  60. #!/usr/bin/python
  61. import sys
  62. print 'Number of arguments:', len(sys.argv), 'arguments.'
  63. print 'Argument List:', str(sys.argv)
  64.  
  65.  
  66. Now run above script as follows −
  67. $ python test.py arg1 arg2 arg3
  68.  
  69. This produce following result −
  70. Number of arguments: 4 arguments.
  71. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']
  72.  
  73. NOTE: As mentioned above, first argument is always script name and it is also being counted in number of arguments.
Advertisement
Add Comment
Please, Sign In to add comment