python - Python:多个参数作为单个参数传递?
问题描述
我正在使用 subprocess 来使用命令行并为另一个脚本传递参数。我有一个参数列表,我把它做成了一个字符串:
import subprocess as sp
arg_list = ["arg1", "arg2", "arg3"]
arg_string = " ".join(arg_list)
现在这适用于以下输出arg1 arg2 arg3
。问题是,当我将它传递给命令行时,它只将其识别为一个参数。
sp.call(["test_file.tcl", arg_string])
** 注意:我call
只使用这个脚本需要 Python 3.4
我知道这只是一个论点,因为将以下内容添加到 .tcl 文件中:
[lindex $argv 0]
[lindex $argv 1]
[lindex $argv 2]
输出是:
arg1 arg2 arg3
. (These are blank lines not dots)
.
这是正确的方法吗?我怎样才能使这项工作真正在 3 个参数而不是 1 个参数中被识别?
** 答:添加shell=True
允许传递字符串而不是列表。
sp.call("test_file.tcl {0}".format(arg_string), shell=True)
解决方案
不要加入args_list
字符串,然后使用 splat 运算符。这将传递['test_file.tcl', 'arg1', 'arg2', 'arg3']
给call
.
sp.call(["test_file.tcl", *arg_list])
根据您的操作系统,您可能还需要通过shell=True
。
推荐阅读
- c# - C# 无法从值(字符串 [])中获取字典键(字符串)
- python - 在 Keras 中,如何使用无维度的 Reshape 图层?
- c# - 从文件 c# 加载图像
- python - 如果值是类似字典的格式,则从熊猫系列中提取数据
- python-3.x - python3烧瓶:如何在html中显示json结果
- ruby-on-rails - Rails HTML 到 PDF 使用 puppeteer(无头 chrome),如何将 html 发送到 puppeteer
- c++ - 如何将 const void* 转换为 unsigned int?
- javascript - 在 JS 中绘制一个时间复杂度最小的带边框的正方形
- python - python WebUI安装上的属性错误
- css - 使用平移变换停止水平滚动