Python Forum

Full Version: Please Help Me About OS Library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys! I'm using python on raspberry pi 4. I'm stucked on this code:


import os

cmd = " vcgencmd measure_temp | egrep -o '[0-9]*\.[0-9]*' "
cputemp = os.system(cmd)



(vcgencmd means the temperature of CPU)

I want to make a code like that:

if cputemp > 50:
print("hot")
First you need to capture the result.  That will be easier with subprocess. Try instead

import subprocess
cmd = "vcgencmd measure_temp | egrep -o '[0-9]*\.[0-9]*'"
result = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True)
ans = int(result.stdout.rstrip()) # or float if it should be a float?
Also, if you're expecting the output to be a number, it has to be converted from the stdout string to the number (presumably either an int() or a float())
(Oct-10-2020, 05:48 PM)bowlofred Wrote: [ -> ]First you need to capture the result.  That will be easier with subprocess. Try instead

import subprocess
cmd = "vcgencmd measure_temp | egrep -o '[0-9]*\.[0-9]*'"
result = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True)
ans = int(result.stdout.rstrip()) # or float if it should be a float?
Also, if you're expecting the output to be a number, it has to be converted from the stdout string to the number (presumably either an int() or a float())

man thank you sooooo much. Heart

can you explain the code if you have time?
What parts are unclear to you?

subprocess.run docs show what arguments it takes.  (Although that leads you to the Popen docs to explain shell= and stdout=).