我们在命令行启动python脚本后,很多时候都会带上脚本的一些参数(比如给argparse模块的参数)。这些参数都是先保存在sys.argv这个list中,然后才给其它模块或功能使用,不过也有一些特别之处。

官方在基础的tutorial中,有一段来解释python命令行的参数传递:

2.1.1. Argument Passing

When known to the interpreter, the script name and additional arguments thereafter are turned into a list of strings and assigned to the argv variable in the sys module. You can access this list by executing import sys. The length of the list is at least one; when no script and no arguments are given, sys.argv[0] is an empty string. When the script name is given as '-' (meaning standard input), sys.argv[0] is set to '-'. When -c command is used, sys.argv[0] is set to '-c'. When -m module is used, sys.argv[0] is set to the full name of the located module. Options found after -c command or -m module are not consumed by the Python interpreter’s option processing but left in sys.argv for the command or module to handle.

没参数时

没有参数时,sys.argv[0]为空字符串,sys.argv的长度最小为1:

$ python3

Python 3.7.3 (default, Jul 3 2019, 10:30:04)

[GCC 7.4.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import sys

>>> print(sys.argv)

['']

python -q 时,跟没有参数一样!sys.argv的第1个元素为空字符串。

参数 - (dash)

执行 python - ...,后面不管有什么都没用了,都将被存入sys.argv,- 表示python通过标准输入(stdin)获得自己的输入,stdin默认是键盘。

$ python3 - a b c d e f

Python 3.7.3 (default, Jul 3 2019, 10:30:04)

[GCC 7.4.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import sys

>>> print(sys.argv)

['-', 'a', 'b', 'c', 'd', 'e', 'f']

.py文件

如果python后面直接跟一个.py文件,这是我们常见的用法,结果请见下面代码:

$ cat sys_argv.py

import sys

print(sys.argv)

$ python3 sys_argv.py

['sys_argv.py']

$ python3 sys_argv.py a b c 1 2 3

['sys_argv.py', 'a', 'b', 'c', '1', '2', '3']

所以,我们常常见到有些代码直接将sys.argv[1:]传给argparse模块,就是这个道理。

如果命令行中的.py文件不在当前路径,sys.argv[0]的值与命令行输入的路径一致,但是会自动展开~扩展符:

$ python3 ~/sys_argv.py

['/home/xinlin/sys_argv.py']

$ python3 ../sys_argv.py

['../sys_argv.py']

$ python3 -c "import sys; print(sys.argv)"

['-c']

sys.argv中只有-c,没有python语句。

这个有点特别,我先将刚才的sys_argv.py文件,在-m的情况下执行:

$ python3 -m sys_argv

['/home/xinlin/test/sys_argv.py']

sys.argv中存放的是模块的pathname。

然后,我有试了试python标准库中的模块:

$ python3 -i -m tkinter

>>> import sys

>>> print(sys.argv)

['python -m tkinter']

>>>

xinlin@ubuntu:~/test$ python3 -i -m http.server

Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

^C

Keyboard interrupt received, exiting.

Traceback (most recent call last):

File "/usr/local/python-3.7/lib/python3.7/http/server.py", line 1235, in test

httpd.serve_forever()

File "/usr/local/python-3.7/lib/python3.7/socketserver.py", line 232, in serve_forever

ready = selector.select(poll_interval)

File "/usr/local/python-3.7/lib/python3.7/selectors.py", line 415, in select

fd_event_list = self._selector.poll(timeout)

KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/usr/local/python-3.7/lib/python3.7/runpy.py", line 193, in _run_module_as_main

"__main__", mod_spec)

File "/usr/local/python-3.7/lib/python3.7/runpy.py", line 85, in _run_code

exec(code, run_globals)

File "/usr/local/python-3.7/lib/python3.7/http/server.py", line 1262, in

test(HandlerClass=handler_class, port=args.port, bind=args.bind)

File "/usr/local/python-3.7/lib/python3.7/http/server.py", line 1238, in test

sys.exit(0)

SystemExit: 0

>>> import sys

>>> print(sys.argv)

['/usr/local/python-3.7/lib/python3.7/http/server.py']

使用 -i 参数,是为了能够在模块运行退出后,进入python解释器,查看sys.argv的值。第1次看到的是一个命令,第2次运行python自带的http服务器,看到的是模块pathname。不知为何有这样的差异。。。

-- EOF --

python sys模块 argv用法_python命令行的参数传递(sys.argv)相关推荐

  1. python乘号的字符代码_python 命令行参数传入 乘号(*) 时出错

    环境 Mac Os X 10.9 python 2.7 实现功能 命令行传入公式,输出结果 出错原因 '*' 号把整个目录下的文件都当作参数传入了 问题 如何正确传入 * 号? 操作 输入公式 '2 ...

  2. cmd打开python显示不是内部_Python命令行窗口提示“不是内部或外部命令……”的解决方法...

    前提:已在[控制面板\系统和安全\系统\高级系统设置\高级\环境变量]里修改变量Path,编辑系统变量后面添加了[;D:\Python27],可以在命令行模式下正常进入Python交互式环境,并能直接 ...

  3. python 命令行 参数_Python命令行参数

    python 命令行 参数 Python Command line arguments are input parameters passed to the script when executing ...

  4. Python命令行解析:sys.argv[]函数的简介、案例应用之详细攻略

    Python命令行解析:sys.argv[]函数的简介.案例应用之详细攻略 目录 sys.argv[]函数的简介 sys.argv[]函数的案例应用 1.基础测试 2.进阶用法 3.sys.argv[ ...

  5. python 命令行解析模块_Python命令行解析模块详解

    python2.7 怎么解析命令行输入的中文参数 本文实例讲述了python读取命令行参数的方法.分享给大家供大家参考.具体分析如下: 如果想对python脚本传参数,python中对应的argc, ...

  6. python 命令行解析函数_python命令行解析之parse_known_args()函数和parse_args()使用区别介绍...

    在python中,命令行解析的很好用, 首先导入命令行解析模块 import argparse import sys 然后创建对象 parse=argparse.ArgumentParser() 然后 ...

  7. python 命令行参数_Python 命令行参数介绍

    Python 提供了 getopt 模块来获取命令行参数. Python 中也可以所用 sys 的 sys.argv 来获取命令行参数: sys.argv 是命令行参数列表. len(sys.argv ...

  8. python命令行参数 空格_Python 命令行参数

    Python 命令行参数 Python 提供了 getopt 模块来获取命令行参数. $ python test.py arg1 arg2 arg3 Python 中也可以使用 sys 的 sys.a ...

  9. python命令行输入参数_Python命令行参数处理

    sys模块 sys模块代表了Python解释器,主要用于获取和Python解释器相关的信息,其中 sys.argv 可以获取命令行参数 在Python交互式解释器中可以先导入sys模块 import ...

最新文章

  1. 基于服务器的AAA作业(第二次)
  2. Sprites实现翻页按钮,圆角,宽度不固定
  3. Adobe奇葩续费机制被网友狂喷:一不留神就扣2500,按月付费还随时取订?长点心吧...
  4. vim 查找并手动替换(笔记)
  5. 聊天工具简单实现(python 半双工聊天)
  6. 后台开发技术--接入层设计
  7. Arcgis desktop 9.3的破解方法_经验版
  8. Angular ERROR NullInjectorError: R3InjectorError(AppModule)的错误分析
  9. rnn神经网络 层次_精讲深度学习RNN三大核心点,三分钟掌握循环神经网络
  10. npm install --save 与 npm install --save-dev 的区别
  11. 字符串 -- 3.15 Length of Last Word -- 图解
  12. 车辆控制-稳态误差分析-前馈
  13. Cmake :创建vs的makefile工程(1)
  14. csu1337 搞笑版费马大定理
  15. 什么软件可以测试音乐速度,酷狗如何测试电台速度
  16. JAVASCRIPT网页特效实例大全pdf
  17. Django中的Model(字段) - 第五轻柔的code - 博客园
  18. 产品经理们都是怎样成为产品经理的?
  19. 成为JAVA(高级)工程师,该学什么
  20. 搜索:多模态搜索算法实践【工业界:将其他模态(视频、音频)的信息降维到文本模态】【学术界:将所有模型信息映射到公共向量空间】

热门文章

  1. Jquery一个简单的点赞效果,实现点赞数+1
  2. StrictMode ——安卓严苛模式
  3. sourceinsight自动参考高亮
  4. 如何在宿舍“优雅”地做寿司?
  5. input type=color 设置颜色
  6. ·乔布斯的接班人Tim Cook是神马人物
  7. 极光推送java实现
  8. 人声计算机怎么调音乐,音乐变成伴奏软件 怎么把歌曲变成伴奏、消除人音
  9. 我的Python心路历程 第十期 (10.2 通达信股票day数据转化为csv)
  10. NowCoder每天下班都会经过万达广场,那里有很多大妈伴随着很high的音乐,在跳广场舞,脖子扭扭,屁股扭扭,乐哉,乐哉! 在广场上,大妈大婶排成 m 排,n列的队伍,其中有一个大妈所在的行中年龄最