0

I want to run this command from python

/opt/vc/bin/vcgencmd measure_temp

when I run it in python it prints the temperature to the screen and returns 0. When I try to assign it to a variable it assigns 0 too.

>>> import os
>>> os.system('/opt/vc/bin/vcgencmd measure_temp')
temp=42.8'C
0
>>> temp=os.system('/opt/vc/bin/vcgencmd measure_temp')
temp=42.8'C
>>> temp
0
>>>

How can I run this command in python and assign the actual temperature to a variable?

MattDMo
  • 100,794
  • 21
  • 241
  • 231
DarylF
  • 716
  • 3
  • 9
  • 24
  • I don't think `os.system` lets you capture the output. You could try I/O redirecting the output to a file, and reading that file in python – inspectorG4dget Jan 24 '14 at 22:28
  • 4
    @inspectorG4dget: or better still, don't use `os.system()`.. – Martijn Pieters Jan 24 '14 at 22:28
  • Or, put another way, the only "output" of [`os.system`](http://docs.python.org/2/library/os.html#os.system) is the exit status; anything the child writes to stdout just goes right through to your own stdout. – abarnert Jan 24 '14 at 22:29
  • @MartijnPieters: Or just read the docs to [`os.system`](http://docs.python.org/2/library/os.html#os.system) instead of just using it blindly, which make the answer obvious… – abarnert Jan 24 '14 at 22:31

1 Answers1

8

os.system doesn't returns the output, use subprocess.check_output for that.

abarnert
  • 354,177
  • 51
  • 601
  • 671
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • 2
    If you look at the `os.system` docs, they explicitly tell you that it has "limitations" and that "the `subprocess` module has more powerful facilities for spawning new processes and retrieving their results", and that "using that module is preferable to using this function." – abarnert Jan 24 '14 at 22:30