Pull a value from a text file to use as a comparitor

Question:

I need the pull the temperature from a text file, and then use the value as a comparitor to run one or the other python script. the data looks like this:

time=14:10
temp=22.4  <-----  I need this value
tempTL=18.7
tempTH=29.9
intemp=66.4

I have seen some examples on how to pull the temperature from this, 22.4 in my example.
I am not sure how to make it a value to compare with.
I need if temp < 30 then run myfile1.py else myfile2.py

sed -n 's/^temp=(.*)/1/p' < realtimegauges.txt
echo temp=`sed -n 's/^temp=(.*)/1/p' < txt`

Not sure how to pass the temp as a varable to compare with.

Asked By: BobW

||

Answers:

Iterate over the input file lines until the needed temp= is found, extract the temperature value, break the file processing and check the needed condition to run a certain file with subprocess.run:

import subprocess

with open('realtimegauges.txt') as f:
    t_val = None
    for line in f:
        if line.startswith('temp='):
            t_val = float(line.split('=')[1])
            break
    if t_val < 30:
        subprocess.run(['python', 'myfile1.py'])
    else:
        subprocess.run(['python', 'myfile2.py'])
Answered By: RomanPerekhrest
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.