First off all, i know this Question was asked before, but its a special case for me, because as the title says i want to redirect the Output of a command to a variable in Python. I know i can use subrocess.popen, but the command i want to run is an array, so i have to use subrocess.call(). I alredy tried to conver the array to a String, but this doen't work for me, because the Command contains spaces. So i need to use subrocess.call() to run my command. But is there a way to get the output of subrocess.call()?
Asked
Active
Viewed 64 times
-1
-
Since you have mentioned it's a special case, can you post the command you want to run? – Ankush Chavan Aug 16 '23 at 10:57
-
I want to Code a Minecraft launcher using minecraft-launcher-lib. The reason why i created this question is, to show the log of the Game in a special window. – pauljako Aug 16 '23 at 11:10
2 Answers
0
You can use subprocess.run()
to capture the output of a command:
import subprocess
command = ["echo", "Hello, World!"]
result = subprocess.run(command, stdout=subprocess.PIPE, text=True)
output = result.stdout.strip()
print("Output:", output)

Mayur Buragohain
- 1,566
- 1
- 22
- 42
-
This will throw an error, In order to execute this command you have to add one more argument in the `run`, whic is `shell=True`. `result = subprocess.run(command, stdout=subprocess.PIPE, shell=True, text=True)` – Elie Hacen Aug 16 '23 at 08:36
-
-
No need to use pipes, just set `capture_output=True` or use `subprocess.check_output()` – Tzane Aug 16 '23 at 11:27
-
1@ElieHacen `shell=True` is required, if you want to run this one Windows – Tzane Aug 16 '23 at 11:29
-1
subprocess.call()
executes a command and waits for it to complete. It returns the exit code of the command. It doesn't capture the output of the command it runs.
SOURCE: Official Guide

Elie Hacen
- 372
- 12