Python Forum
How to excess external program and get a value back?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to excess external program and get a value back?
#1
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!
Reply
#2
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)
Reply
#3
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
Reply
#4
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)
Reply
#5
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])
Reply
#6
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.
Reply
#7
(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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#8
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I can't read data from the clipboard in an external program AlekseyPython 1 2,576 Mar-19-2019, 07:56 AM
Last Post: buran
  How can I move in an external program via Python? Pythoner 0 2,243 Dec-29-2018, 12:43 PM
Last Post: Pythoner
  Embedding Args in external program call brizflysdrones 5 4,840 May-09-2017, 08:29 PM
Last Post: buran
  input-external program-output baolevani 2 3,878 Mar-27-2017, 04:34 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020