Upload MQTT Free Space Information

Python auf Einplatinencomputer wie Raspberry Pi, Banana Pi / Python für Micro-Controller
Antworten
zeron850
User
Beiträge: 8
Registriert: Mittwoch 17. Juni 2020, 08:13

Hallo Zusammen,

ich habe folges Skript erstellt damit dieses den Upload des freien Speichers der SD Card zu einem MQTT Broker durchführt:

Code: Alles auswählen

[#!/usr/bin/env python3
import paho.mqtt.publish as publish
from subprocess import check_output
from re import findall

def get_space():
    space = check_output(df -P / | tr -s " " " " | cut -d " " -f 4).decode("UTF-8")
    return(findall("\d+\.\d+",space)[0])

def publish_message(topic, message):
    print("Publishing to MQTT topic: " + topic)
    print("Message: " + message)

    publish.single(topic, message, hostname="192.168.1.31")

temp = get_space()
publish_message("Home/pi/Space", space)


Allerdings erhalte ich folgende Fehlermeldung:

df: Ungültige Option --
„df --help“ liefert weitere Informationen.
Traceback (most recent call last):
File "/home/pi/space.py", line 16, in <module>
temp = get_space()
File "/home/pi/space.py", line 7, in get_space
space = check_output(["df","-P / | tr -s " " " " | cut -d " " -f 4"]).decode("UTF-8")
File "/usr/lib/python3.7/subprocess.py", line 395, in check_output
**kwargs).stdout
File "/usr/lib/python3.7/subprocess.py", line 487, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['df', '-P / | tr -s | cut -d -f 4']' returned non-zero exit status 1.


Nomral lauter der Befehl zur Abfrage:

Code: Alles auswählen

 df -P / | tr -s " " " " | cut -d " " -f 4 

An was könntes es liegen?

Danke & Grüße
Thomas
Zuletzt geändert von zeron850 am Sonntag 2. August 2020, 09:29, insgesamt 3-mal geändert.
__deets__
User
Beiträge: 14538
Registriert: Mittwoch 14. Oktober 2015, 14:29

Subprocess kann keine vollen Shell-Kommandos ausfuehren. Installier dir einfach das psutil-Modul, und du kannst auf diese Werte direkt zugreifen.
Sirius3
User
Beiträge: 17748
Registriert: Sonntag 21. Oktober 2012, 17:20

Der Code in der Fehlermeldung weicht von gezeigten ab. In check_output kann man nicht einfach irgendwelche Shell-Befehle übergeben. Es wäre auch deutlich lesbar, das Parsen des Outputs in Python zu schreiben. Jedes Argument muss ein eigenes Listenelement sein

Code: Alles auswählen

check_output(["df","-P", "/"])
zeron850
User
Beiträge: 8
Registriert: Mittwoch 17. Juni 2020, 08:13

Ok, Danke für eure Hilfe!
Antworten