Search code examples
pythonwindowscmdio-redirectionfastboot

Commands output prints on terminal and stdout is empty. How i can store the output or redirect to stdout?


I have a python program with following line of code. Host OS: Windows

output = subprocess.Popen("fastboot -s " + deviceId + " getvar slot-count",
                              shell=True, stdout=subprocess.PIPE, universal_newlines=True)
stdout, stderr = output.communicate(timeout=600)

It prints out the output on console and stdout is empty. How i can redirect output to stdout or store it in variable?

I have tried redirecting to a file but any continuation of command with ">" , "|" to redirect is passed on to fastboot program as argument and runs into error.

I have tried suprocess.Popen stdout argument as tempfile to check if subprocess.PIPE is not able to buffer.

i have tried os.system(fastboot -s " + deviceId + " getvar slot-count > output.txt"), output.txt is empty.


Solution

  • Maybe your fastboot program writes to stderr rather than stdout. Test it like this:

    fastboot PARAMETERS > stdout.txt 2> stderr.txt
    

    and inspect the files stdout.txt and stderr.txt. If the output is in stderr.txt change your subprocess() call accordingly.