簡體   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