0

I am wrapping a GUI tool around a command line tool in python and I am using subprocess module.

process = subprocess.Popen(
    args, stdout=subprocess.PIP, encoding='utf8',
)

while True:
    output = process.stdout.readline()
    if output == b"" and process.poll() is not None:
        print("BREAKING")
        break
    if output:
        print(f'stdout: {output.strip()}')
result_code = process.poll()
print(f"result_code: {result_code }")

I am trying to read all output from the console, but with subprocess.PIPE I can only read part of it.

Below is output from wrapper script using subprocess:

Output using subprocess

Below is output I want to read:

Output fom using command line tool directly

Using the stdout=subprocess.PIPE, it does not include the Download 9%, Download 12% etc.

How can I also capture this sys output?

I've tried using stdout=sys.stdout, this will output evertything but I can't read from it like process.stdout.readline() to handle the output.

Xiddoc
  • 3,369
  • 3
  • 11
  • 37
jtm101
  • 1
  • 2
  • In your Terminal, try sending `stdout` and `stderr` to different places so you can see what is directed where and what you need to capture. `./someCommand > stdout.txt 2> stderr.txt` – Mark Setchell Dec 20 '22 at 12:06
  • You may also get less information printed when stderr doesn't go direct to a terminal as a deliberate implementation decision in the program you're running, but stderr would be my first bet; it's supposed to be used for all diagnostic logs and everything else that isn't output as such. Status information is certainly diagnostic in nature, so stderr is not just errors. – Charles Duffy Dec 20 '22 at 12:47
  • Anyhow, if the data isn't on stderr either, it's the program you're wrapping, not Python, that makes this decision. Look at its implementation and docs. – Charles Duffy Dec 20 '22 at 12:51
  • (can you trick it into thinking its output goes to a TTY? Yes, and we have existing Q&A describing how to do that, but look for a configuration flag first; if it's open source, maybe patch it to add such a flag). – Charles Duffy Dec 20 '22 at 12:53
  • If that's the route you need to go, this is a duplicate of https://stackoverflow.com/questions/67533909/python-3-can-i-run-a-subprocess-and-obtain-its-output-without-it-knowing-that. If capturing stderr is enough, then this is a duplicate of https://stackoverflow.com/questions/72918072/how-to-capture-both-stdout-and-stderr-at-the-same-using-subprocess-popen – Charles Duffy Dec 20 '22 at 13:00

0 Answers0