Home > Mobile >  How to wait until a certain output (string) occurs while executing subprocess.check_output?
How to wait until a certain output (string) occurs while executing subprocess.check_output?

Time:12-19

I am trying to write a Python script which will download my software build. So the script will wait until certain output occurs and proceed for the next task:

import os
import subprocess

out = subprocess.check_output(["wget", "--downloadlink--"])
print(out)
if "saved" in out:
    print("Download completed!!")

It's not printing any output. It's simply executing the command and exiting.

I tried the above program but didn't get any desired result. Any other way of doing it?

CodePudding user response:

The recommended way is to use subprocess.run. However if it's not available. The key point is to tell the method to output STDERR to get access to the message.

import subprocess

out = subprocess.check_output(
    [
        "wget",
        "--downloadlink--",
    ],
   stderr=subprocess.STDOUT
)

if "saved" in str(out, "utf-8"):
    print("saved !!")
# saved !!

Note: I don't think is the right way to check if the download of a file is OK. This approach is weak because it's rely on a text string in the output. It should be better to use at least the return code of wget or even better performing the download directly through a Python lib. However I do not know your constraints.

  • Related