-1

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()?

gre_gor
  • 6,669
  • 9
  • 47
  • 52
pauljako
  • 25
  • 5
  • 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 Answers2

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
-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.

enter image description here

SOURCE: Official Guide

Elie Hacen
  • 372
  • 12