Nachdem ich mal wieder verzweifle, frage ich nochmal hier.
Ich möchte mit Python eine Datei mit einer Ganzzahl öffnen und vergleichen, aber egal was ich anstelle, er weigert sich, alles googlen hilft nicht.
Code
avg_cpu = int(text_avg_cpu)
TypeError: int() argument must be a string, a bytes-like object or a number, not '_io.TextIOWrapper'
Code:
Python
#!/usr/bin/env python3
import subprocess
import time
import psutil
from pathlib import Path
max_cpu = 20
min_cpu = 19
while True:
fname = Path("/dev/shm/cpu_avg.txt")
time.sleep(1)
print(fname.exists())
if fname.is_file() == True:
with open("/dev/shm/cpu_avg.txt", "r") as text_avg_cpu:
int(text_avg_cpu.read())
avg_cpu = int(text_avg_cpu())
print(avg_cpu)
time.sleep(1)
if avg_cpu < min_cpu:
print("{} ist weniger als {}".format(avg_cpu, min_cpu))
print("Prozess läuft nicht, ich starte ihn")
subprocess.run("/home/pi/start.sh")
with open("/dev/shm/Prozesscheck.txt", "w") as avg_cpu:
avg_cpu.write("1")
data = avg_cpu.read()
print(data)
time.sleep(1)
else:
print("{} ist mehr als {}".format(avg_cpu, min_cpu))
print("Prozess läuft")
with open("/dev/shm/Prozesscheck.txt", "r") as f:
avg_cpu.read()
data = f.read()
print(data)
time.sleep(1)
Alles anzeigen
Warum hilft das int() hier nichts ?
EDIT: () vergessen, ändert die Fehlermeldung in: