使用参数从 subprocess.call 调用应用程序 [英] Calling app from subprocess.call with arguments

查看:62
本文介绍了使用参数从 subprocess.call 调用应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 初学者,我一直在尝试调用命令行应用程序,但它失败了:

<预><代码>>>>将子进程导入为 s>>>s.call("gpio -g 读取 17")回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件/usr/lib/python2.6/subprocess.py",第 470 行,调用中返回 Popen(*popenargs, **kwargs).wait()文件/usr/lib/python2.6/subprocess.py",第 623 行,在 __init__ 中错误读取,错误写入)文件/usr/lib/python2.6/subprocess.py",第 1141 行,在 _execute_child 中引发 child_exceptionOSError: [Errno 2] 没有那个文件或目录

但是如果我添加 shell=True 一切都会开始工作.有人能解释一下原因吗?

<预><代码>>>>将子进程导入为 s>>>s.call("gpio -g 读取 17", shell=True)>>>0

解决方案

您没有使用呼叫权限.查看介绍或文档中的任何示例.call 的第一个参数是args",一个参数序列,其中 arg[0] 是要运行的程序.

所以,当你这样做时:

s.call("gpio -g 读取 17")

子进程可以通过两种方式来解释这一点.它应该运行一个名为g"的程序,参数为p"、i"、o"、"等(记住,字符串是字符序列.)它可能会运行一个名为gpio -g read"的程序17" 没有额外的参数.无论哪种方式,它都不会找到这样的程序.(除非您的 PATH 中碰巧有一个名为g"或gpio -g read 17"的程序,在这种情况下,它会做错事而不是给您错误……)

你想要的是:

s.call(["gpio", "-g", "read", "17"])

那么,如果您传递 shell=True,为什么这会起作用?因为这整个字符串被传递给 shell,shell 然后自己解析命令行并用空格分隔.这就像调用 os.system("gpio -g read 17").

请注意,以上所有内容都有些过于简单化(它忽略了 Windows,并且 shell 解析实际上不仅仅是用空格分隔"等等),因此您应该实际阅读文档.(另外,写subprocess 文档的人比我写得更好.)

I'm a beginner in Python, and I've been trying to call a command line app, but it fails:

>>> import subprocess as s
>>> s.call("gpio -g read 17")
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python2.6/subprocess.py", line 470, in call
        return Popen(*popenargs, **kwargs).wait()
    File "/usr/lib/python2.6/subprocess.py", line 623, in __init__
        errread, errwrite)
    File "/usr/lib/python2.6/subprocess.py", line 1141, in _execute_child
        raise child_exception
OSError: [Errno 2] No such file or directory

But then if I add shell=True it all starts working. Can someone explain why?

>>> import subprocess as s
>>> s.call("gpio -g read 17", shell=True)
>>> 0

解决方案

You're not using call right. Look at the introduction or any of the examples in the docs. The first argument of call is "args", a sequence of arguments, where arg[0] is the program to run.

So, when you do this:

s.call("gpio -g read 17")

There are two ways subprocess could interpret this. It should run a program called "g" with arguments "p", "i", "o", " ", etc. (Remember, strings are sequences of characters.) It might instead run a program called "gpio -g read 17" with no additional arguments. Either way, it's not going to find such a program. (Unless you happen to have a program called "g" or "gpio -g read 17" on your PATH, in which case it'll do the wrong thing instead of giving you an error…)

What you want is:

s.call(["gpio", "-g", "read", "17"])

So, why does this work if you pass shell=True? Because this whole string gets passed to the shell, which then does its own parsing of the command line and separates things by spaces. It's like calling os.system("gpio -g read 17").

Please note that all of the above is a bit oversimplified (it ignores Windows, and shell parsing isn't really just "separate by spaces", and so on), so you should actually read the documentation. (Also, whoever wrote the subprocess docs is a better writer than me.)

这篇关于使用参数从 subprocess.call 调用应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆