(1)sys.argv是什麼html
sys模塊爲進入解釋器維護或使用的變量,以及與解釋器相關的函數提供了途徑。sys.argv在腳本程序中扮演了這樣一個角色:將命令行輸入的參數做爲一個list傳入腳本程序,即咱們能夠在cmd命令形式驅動程序方法下,將設定的參數傳入腳本,argv[0]是驅動腳本的名字(由操做系統來決定是不是全路徑的名字),若是cmd中,將-c傳入,那麼argv[0]就是-c,python
分界線中的內容爲cmd驅動腳本的詳細解釋,而關於cmd,-c傳入的栗子,本文再也不贅述 詳見https://jingyan.baidu.com/article/03b2f78c3b38f05ea237ae05.htmlapp
在解釋中,咱們看到py [launcher-args] [python-args] script [script-args],這裏script是腳本名字,而其後的[script-args]正是要傳入腳本中sys.argv的列表,從argv[1]開始。下面舉個簡單的例子:dom
import sys a=sys.argv[0] b=sys.argv[1] c=sys.argv[2] print('argv0:%s'% a) print('argv1:%s'% b) print('argv2:%s'%c)
以上代碼命名爲sys_argvTest.py函數
而後在cmd中運行:ui
能夠看到在腳本名字 後面的'thisisargv1' 'thisisargv2'分別傳入了腳本中的sys.argv[1],sys.argv[2]中this
---------------------------------------------------------------------------------------這是一道華麗的分界線--------------------------------------------------------------------------------------------------------------------------spa
usage:
py [launcher-args] [python-args] script [script-args]
Launcher arguments:
-2 : Launch the latest Python 2.x version
-3 : Launch the latest Python 3.x version
-X.Y : Launch the specified Python version
The above all default to 64 bit if a matching 64 bit python is present.
-X.Y-32: Launch the specified 32bit Python version
-X-32 : Launch the latest 32bit Python X version
-X.Y-64: Launch the specified 64bit Python version
-X-64 : Launch the latest 64bit Python X version
-0 --list : List the available pythons
-0p --list-paths : List with paths
The following help text is from Python:
usage: C:\Users\Options and arguments (and corresponding environment variables):
-b : issue warnings about str(bytes_instance), str(bytearray_instance)
and comparing bytes/bytearray with str. (-bb: issue errors)
-B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x
-c cmd : program passed in as string (terminates option list)
-d : debug output from parser; also PYTHONDEBUG=x
-E : ignore PYTHON* environment variables (such as PYTHONPATH)
-h : print this help message and exit (also --help)
-i : inspect interactively after running script; forces a prompt even
if stdin does not appear to be a terminal; also PYTHONINSPECT=x
-I : isolate Python from the user's environment (implies -E and -s)
-m mod : run library module as a script (terminates option list)
-O : remove assert and __debug__-dependent statements; add .opt-1 before
.pyc extension; also PYTHONOPTIMIZE=x
-OO : do -O changes and also discard docstrings; add .opt-2 before
.pyc extension
-q : don't print version and copyright messages on interactive startup
-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE
-S : don't imply 'import site' on initialization
-u : force the stdout and stderr streams to be unbuffered;
this option has no effect on stdin; also PYTHONUNBUFFERED=x
-v : verbose (trace import statements); also PYTHONVERBOSE=x
can be supplied multiple times to increase verbosity
-V : print the Python version number and exit (also --version)
when given twice, print more information about the build
-W arg : warning control; arg is action:message:category:module:lineno
also PYTHONWARNINGS=arg
-x : skip first line of source, allowing use of non-Unix forms of #!cmd
-X opt : set implementation-specific option
--check-hash-based-pycs always|default|never:
control how Python invalidates hash-based .pyc files
file : program read from script file
- : program read from stdin (default; interactive mode if a tty)
arg ...: arguments passed to program in sys.argv[1:]
Other environment variables:
PYTHONSTARTUP: file executed on interactive startup (no default)
PYTHONPATH : ';'-separated list of directories prefixed to the
default module search path. The result is sys.path.
PYTHONHOME : alternate <prefix> directory (or <prefix>;<exec_prefix>).
The default module search path uses <prefix>\python{major}{minor}.
PYTHONCASEOK : ignore case in 'import' statements (Windows).
PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.
PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.
PYTHONHASHSEED: if this variable is set to 'random', a random value is used
to seed the hashes of str, bytes and datetime objects. It can also be
set to an integer in the range [0,4294967295] to get hash values with a
predictable seed.
PYTHONMALLOC: set the Python memory allocators and/or install debug hooks
on Python memory allocators. Use PYTHONMALLOC=debug to install debug
hooks.
PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale
coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of
locale coercion and locale compatibility warnings on stderr.
PYTHONBREAKPOINT: if this variable is set to 0, it disables the default
debugger. It can be set to the callable of your debugger of choice.
PYTHONDEVMODE: enable the development mode.
---------------------------------------------------------------------------------------這是一道華麗的分界線-------------------------------------------------------------------------------------------------------------------------