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

我们在命令行启动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 --

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值