Avatar ·

Redirecting terminal output into a variable

📁 Python, Frage, Beispiel

Hilfe, um die Umleitung der CMD-Ausgabe in eine Variable zu verstehen, da der Variable nur der Exit_Code des Prozesses zugewiesen wird. Zur Verdeutlichkeit gibt es ein paar Beispiele: Beispiel 1

import sys, os command = "tasklist" result1 = os.system(command) print (result1) # Ausgabe: 0

Beispiel 2

command = "tasklist > temp.txt" result2 = os.system(command) print (result2) # Ausgabe: 0 with open("temp.txt", "r") as f: print(f.read())