Python Forum

Full Version: How to excess external program and get a value back?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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!
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)
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
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)
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])
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.
(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
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.