浅谈Python之sys.argv
(1)sys.argv是什么
sys模块为进入解释器维护或使用的变量,以及与解释器相关的函数提供了途径。sys.argv在脚本程序中扮演了这样一个角色:将命令行输入的参数作为一个list传入脚本程序,即我们可以在cmd命令形式驱动程序方法下,将设定的参数传入脚本,argv[0]是驱动脚本的名字(由操作系统来决定是否是全路径的名字),如果cmd中,将-c传入,那么argv[0]就是-c,
分界线中的内容为cmd驱动脚本的详细解释,而关于cmd,-c传入的栗子,本文不再赘述 详见https://jingyan.baidu.com/article/03b2f78c3b38f05ea237ae05.html
在解释中,我们看到py [launcher-args] [python-args] script [script-args],这里script是脚本名字,而其后的[script-args]正是要传入脚本中sys.argv的列表,从argv[1]开始。下面举个简单的例子:
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中运行:
可以看到在脚本名字 后面的'thisisargv1' 'thisisargv2'分别传入了脚本中的sys.argv[1],sys.argv[2]中
---------------------------------------------------------------------------------------这是一道华丽的分界线--------------------------------------------------------------------------------------------------------------------------
py [launcher-args] [python-args] script [script-args]
-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
-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:]
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.
最新文章
- Win Form程序线程点点
- Integer Inquiry
- 中国特色社会主义的体制中有这样的现象:地方省政府要坚持党的领导和按 照国务院的指示进行安全生产。请编写一个java应用程序描述上述的体制现象。
- Word文献类型标志
- 控制台程序使用MFC类的方法
- [转]web调试工具总结(firebug/fidder/httpwatch/wireshark)
- 数据库还原失败System.Data.SqlClient.SqlError: 无法执行 BACKUP LOG,因为当前没有数据库备份
- Mybatis——choose, when, otherwise可以达到switch case效果
- 私有云的难处—为什么需要CloudEngine?
- 贝叶斯公式与最大后验估计(MAP)
- Hadoop学习笔记(六):hive使用
- 在内网使用Gradle构建Android Studio项目
- vim安装自动补全插件
- 【iCore1S 双核心板_ARM】例程十一:RTC实时时钟实验——显示时间和日期
- linux 设置与网络同步的时间
- w3school 基础学习
- 新的Blog
- git 下载代码
- 前端mvc mvp mvvm 架构介绍(vue重构项目一)
- Qt 学习之路 2(71):线程简介