Python子进程调用返回“找不到命令”,终端正确执行

2024-06-16 16:38:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从python运行gphoto2,但是没有成功。它只返回未找到的命令。 gphoto安装正确,如中所示,命令在终端中工作正常。

p = subprocess.Popen(['gphoto2'], shell=True, stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT, executable='/bin/bash')

for line in p.stdout.readlines():
    print line
p.wait()

/bin/bash: gphoto2: command not found

我知道osx终端(app)有点搞笑,但是,我对osx的了解很少。

你觉得这个怎么样?

编辑

更改了一些代码,出现了其他错误

p = subprocess.Popen(['gphoto2'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout:
    print line


    raise child_exception
OSError: [Errno 2] No such file or directory

编辑

使用完整路径'/opt/local/bin/gphoto2'

但是,如果有人想解释使用哪个shell或者如何登录并能够拥有相同的功能。。?


Tags: in命令bash终端forbinstderrstdout
1条回答
网友
1楼 · 发布于 2024-06-16 16:38:34

使用shell = True时,subprocess.Popen的第一个参数应该是字符串,而不是列表:

p = subprocess.Popen('gphoto2', shell=True, ...)

但是,如果可能的话,应该避免使用shell = True,因为它可以是security risk(参见警告)。

所以用

p = subprocess.Popen(['gphoto2'], ...)

(当shell = False,或者如果省略了shell参数,则第一个参数应该是一个列表。)

相关问题 更多 >