Python Forum
Please Help Me About OS Library
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please Help Me About OS Library
#1
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")
Reply
#2
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())
Reply
#3
(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?
Reply
#4
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=).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PyInstaller, how to create library folder instead of library.zip file ? harun2525 2 4,817 May-06-2017, 11:29 AM
Last Post: harun2525

Forum Jump:

User Panel Messages

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