繁体   English   中英

Python 3:使用子进程通过gphoto2拍照,但是无法设置自定义文件名。

[英]Python 3: Using subprocess to take photo via gphoto2 but can't set custom filename doing it.

当我通过终端进行操作时,一切正常,但是当我使用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脚本和子进程来执行此操作时,没有任何反应。 我试图这样做:

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

和:

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它可以正常工作,但是却得到了我不想使用的文件名。 你能告诉我我做错了吗?

在命令行上,引号和空格由外壳使用; 如果shell=False ,则需要自己在shlex分割标记(并且最好了解shell如何处理引号;或使用shlex为您完成工作)。

import subprocess

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

除非您坚持使用真正的supbrocess.Popen() Python版本,否则应避免使用supbrocess.Popen() ,而supbrocess.Popen() subprocess.run() (或者对于稍旧的Python版本,则使用subprocess.check_output() )。 较低级别的Popen()接口比较笨拙,但是当较高级别的API无法满足您的要求时,它会为您提供低级别的访问控制。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM