将字符串与文字字符串插值用作Python子进程的参数
我正在尝试使用Python子过程运行Shell执行(XcodeBuild
)。
所需的命令如下所示:
xcodebuild -workspace SomeWorkspace.xcworkspace -scheme SomeScheme ...
它需要几个带有值的参数,我尝试使用字符串插值来创建它们。
args=[
"xcodebuild",
f"-workspace {workspace}",
f"-scheme {scheme}",
...
]
但是,当我将它们传递到subprocess
时,xcodebuild
失败了以下消息:
xcodebuild: error: invalid option '-workspace SomeWorkspace.xcworkspace'
如果我将这些关键字值配对分开并将它们附加到列表中,则一切都按预期工作。
因此,我猜Python子过程将每个参数元素视为“单个”参数,即使它包含空间。但是我想知道Python如何解释这些论点并将其提供给外壳。 f标准字符串的处理方式有所不同吗?最终,这只是一个字符串...
我将这些论点传递给子过程如下:
process = subprocess.Popen(
args=args,
shell=False,
...
我在 python doc 完全指的是这种情况:
特别注意,选项(例如 - 输入)和参数(例如eggs.txt)在外壳中被alleppace隔开,而在单独的列表元素中,而在外壳中使用时需要引用或反斜线逃脱的参数(例如包含空格的文件名或上面显示的回声命令)是单个列表元素。
因此,这是一种预期的行为,我必须阅读shlex.split()
以获取更多详细信息。
呢Python Doc很难阅读(尤其是对于像我这样的非英语人)。感谢您的帮助。
I'm trying to run shell execution (xcodebuild
) with python subprocess.
The desired command is something like below:
xcodebuild -workspace SomeWorkspace.xcworkspace -scheme SomeScheme ...
It takes several arguments with values, and I tried to create them using literal string interpolation.
args=[
"xcodebuild",
f"-workspace {workspace}",
f"-scheme {scheme}",
...
]
But when I pass them to subprocess
, xcodebuild
fails with following message :
xcodebuild: error: invalid option '-workspace SomeWorkspace.xcworkspace'
If I split those keyword-value pairs and appending them to the list, everything works as expected.
So I guess that python subprocess treats each argument elements as a 'single' argument even if it contains space. But I'm wondering how python interpret those arguments and supply them to shell. Is f-literal string treated differently? It's just a string, eventually...
I'm passing these arguments to subprocess as below:
process = subprocess.Popen(
args=args,
shell=False,
...
I found on the python doc which refers exactly this case :
Note in particular that options (such as -input) and arguments (such as eggs.txt) that are separated by whitespace in the shell go in separate list elements, while arguments that need quoting or backslash escaping when used in the shell (such as filenames containing spaces or the echo command shown above) are single list elements.
So this is an intended behavior and I'd have to read shlex.split()
for more detail.
! Python doc is somewhat difficult to read(especially for non-English person like myself). Thanks for all your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
args
非常特别地,每个列表元素最终都会向您呼叫的程序中一个参数。在您的示例中,您最终只有2个参数,就像' - workspace a_workspace'
和' - scheme a_scheme'
。可能有助于您调试此功能的方法是针对测试Python程序进行测试,该程序可以向您展示其视为参数的内容:
我们将其称为
arg_test.py
然后,您在程序中进行:
您会看到:
修复:
修复程序后,测试程序应输出:
args
is very particular that each list element ends up a single argument to the program you're calling. In your example it you end up with just 2 arguments that would be like'-workspace a_workspace'
and'-scheme a_scheme'
.What might help you in debugging this is to test against a testing python program that can show you what it is seeing as arguments:
We'll call it
arg_test.py
Then, you do in your program:
You'll see:
Your fix:
once you make the fix, the test program should output: