为什么在 python 中打印参数不起作用?
Why printing arguments in python doesn't work?
我正在尝试向我的朋友解释 sys.argv 的工作原理。我 运行 来自命令行的这个片段(在 cmd 和 bash 上测试)。
python -c "import sys; print(sys.argv)" bla stas
但令我惊讶的是,这是输出:
['-c', 'bla', 'stas']
我很困惑 - 为什么引号 "import sys; print(sys.argv)"
中的表达式不是参数列表的一部分?不应该是这样的第二名吗:
['-c', '"import sys; print(sys.argv)"', 'bla', 'stas']
?
argv[0]
is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c
command line option to the interpreter, argv[0]
is set to the string '-c'
.
将命令本身也放在 argv[]
中是错误的,因为它不是脚本的参数。参数只是 bla
和 stas
.
Python 将 -c
参数视为特例,不会将代码字符串放入 sys.argv
.
我正在尝试向我的朋友解释 sys.argv 的工作原理。我 运行 来自命令行的这个片段(在 cmd 和 bash 上测试)。
python -c "import sys; print(sys.argv)" bla stas
但令我惊讶的是,这是输出:
['-c', 'bla', 'stas']
我很困惑 - 为什么引号 "import sys; print(sys.argv)"
中的表达式不是参数列表的一部分?不应该是这样的第二名吗:
['-c', '"import sys; print(sys.argv)"', 'bla', 'stas']
?
argv[0]
is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the-c
command line option to the interpreter,argv[0]
is set to the string'-c'
.
将命令本身也放在 argv[]
中是错误的,因为它不是脚本的参数。参数只是 bla
和 stas
.
Python 将 -c
参数视为特例,不会将代码字符串放入 sys.argv
.