python命令行的参数传递(sys.argv)

2020年1月16日 / 129次阅读 / Last Modified 2020年1月31日
Python命令行sys模块

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

参数 -c

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

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

参数 -m

这个有点特别,我先将刚才的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 <module>
    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 --

本文链接:https://www.pynote.net/archives/1744

留言区

《python命令行的参数传递(sys.argv)》有1条留言

电子邮件地址不会被公开。 必填项已用*标注

  • 麦新杰

    python3 -h,可以看到:

    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:]
     [回复]


前一篇:
后一篇:

More


©Copyright 麦新杰 Since 2019 Python笔记

go to top