当我通过终端来做的时候,一切都很好,但是当我使用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
它工作得很好,但是我得到了我不想要的文件名。你能告诉我我做错了什么吗?!在
在命令行上,引号和空格由shell使用;使用} 为您完成这项工作)。在
shell=False
您需要自己拆分空白上的标记(最好理解shell如何处理引号;或者使用^{除非你被困在一个真正旧石器时代的Python版本上,否则你应该避免使用
supbrocess.Popen()
,而使用subprocess.run()
(或者,对于稍旧的Python版本,subprocess.check_output()
)。较低级别的Popen()
接口很难处理,但是当高级API不能执行您想要的操作时,它为您提供了低级访问控制。在相关问题 更多 >
编程相关推荐