Python Forum
How to excess external program and get a value back? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to excess external program and get a value back? (/thread-10880.html)



How to excess external program and get a value back? - hhanswurster - Jun-11-2018

Hey Guys,

i want to get a value from my camera to write it to a log file. I am using gphoto2, thats the command and the output.

pi@raspberrypi:~ $ gphoto2 --get-config /main/capturesettings/shutterspeed
Label: Shutter Speed                                                           
Type: RADIO
Current: 1/8
pi@raspberrypi:~ $ 
i want "1/8" to be stored as "sspeed", so i can use it later in my logging.

call(["gphoto2","--get-config","/main/capturesettings/shutterspeed"])
should be fine, but how to get the value 1/8 parsed ?

thanks for your help!


RE: How to excess external program and get a value back? - snippsat - Jun-11-2018

Use check_output.
It will return output as a whole bytes object so can decode(to string) and split at '/r/n' to get lines.
Example untested:
from subprocess import check_output

command = check_output(["gphoto2", "--get-config", "/main/capturesettings/shutterspeed"])
output = command.decode().strip()
sspeed = output.split('\r\n')[2]

with open('sspeed.txt') as f_out:
    f_out.write(sspeed)



RE: How to excess external program and get a value back? - hhanswurster - Jun-12-2018

hmmm sadly that does not work :(

Traceback (most recent call last):
  File "timelapse2.py", line 99, in <module>
    sspeed = output.split('\r\n')[2]
IndexError: list index out of range



RE: How to excess external program and get a value back? - snippsat - Jun-12-2018

What's dos the command and output return?
from subprocess import check_output
 
command = check_output(["gphoto2", "--get-config", "/main/capturesettings/shutterspeed"])
output = command.decode().strip()
print(command)
print(output)



RE: How to excess external program and get a value back? - hhanswurster - Jun-12-2018

Label: Shutter Speed
Type: RADIO
Current: 1/8

Label: Shutter Speed
Type: RADIO
Current: 1/8
with
command = check_output(["gphoto2","--get-config","/main/capturesettings/shutterspeed"])
output = command.decode().strip()
print output.split(' ')
i got:

[u'Label:', u'Shutter', u'Speed\nType:', u'RADIO\nCurrent:', u'1/8']

now i have to print only the last one somehow :D

got it :)

command = check_output(["gphoto2","--get-config","/main/capturesettings/shutterspeed"])
output = command.decode().strip()
output = output.split(' ')
print(output[-1])



RE: How to excess external program and get a value back? - snippsat - Jun-12-2018

It work's,you should have mention that you use python 2.
We do amuse(and advise) that most people now should use Python 3.5 or higer.


RE: How to excess external program and get a value back? - volcano63 - Jun-12-2018

(Jun-11-2018, 11:28 PM)snippsat Wrote:
...
sspeed = output.split('\r\n')[2]
...

That is for Windows, OP is on Ubuntu

sspeed = output.splitlines()[2]
should work in any OS


RE: How to excess external program and get a value back? - hhanswurster - Jun-12-2018

hey,
oh sorry. actually i did not know at all that there were different versions.
i started a few hours ago :D

thanks for your advice, i will do my research.