Python3:使用子进程通过gphoto2拍照,但不能设置自定义文件名。

2024-06-16 19:03:59 发布

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

当我通过终端来做的时候,一切都很好,但是当我使用python脚本时,它就不工作了

命令: gphoto2 --capture-image-and-download --filename test2.jpg

New file is in location /capt0000.jpg on the camera                            
Saving file as test2.jpg
Deleting file /capt0000.jpg on the camera

我都很好。 但当我试图通过python脚本和子进程来实现它时,什么都没有发生。我试着这样做:

^{pr2}$

以及:

import subprocess
test = subprocess.Popen(["gphoto2", "--capture-image-and-download --filename'test2.jpg'"], stdout=subprocess.PIPE)
output = test.communicate()[0]
print(output)

虽然我只使用--capture-image-and-download它工作得很好,但是我得到了我不想要的文件名。你能告诉我我做错了什么吗?!在


Tags: andtheimage脚本ondownloadfilenamefile
1条回答
网友
1楼 · 发布于 2024-06-16 19:03:59

在命令行上,引号和空格由shell使用;使用shell=False您需要自己拆分空白上的标记(最好理解shell如何处理引号;或者使用^{}为您完成这项工作)。在

import subprocess

test = subprocess.Popen([
        "gphoto2",
        " capture-image-and-download",
        " filename", "test2.jpg"],
    stdout=subprocess.PIPE)
output = test.communicate()[0]
print(output)

除非你被困在一个真正旧石器时代的Python版本上,否则你应该避免使用supbrocess.Popen(),而使用subprocess.run()(或者,对于稍旧的Python版本,subprocess.check_output())。较低级别的Popen()接口很难处理,但是当高级API不能执行您想要的操作时,它为您提供了低级访问控制。在

相关问题 更多 >