Issue in using "subprocess.Popen" for parsing the command output

MRAB python at mrabarnett.plus.com
Sun Nov 25 13:13:55 EST 2018


On 2018-11-25 17:54, srinivasan wrote:
> Hope now I have changed on the string output as below, could you 
> please correct me if am still wrong?
>
> import sys
> import subprocess
>
> interface = "wlan0"
>
>
> def main(ssid, pw):
>
>     try:
>         cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw)
>
>         proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, 
> stderr=subprocess.PIPE, shell=True, universal_newlines=True)
>         stdout, stderr = proc.communicate()
>         retcode = proc.returncode
>
>         print("printing stdout!!!!!!!!!!", stdout)
>         print("printing retcode!!!!!!!!!!", retcode)
>
>     except subprocess.CalledProcessError as e:
>         s = """While executing '{}' something went wrong.
>                         Return code == '{}'
>                         Return output:\n'{}'
>                         """.format(cmd, e.returncode, e.output, 
> shell=True)
>         raise AssertionError(s)
>
>     #return proc.strip().decode("utf-8")
> *  return proc.decode("utf-8").strip()*
>
[snip]

No. As I said in _my_ post, 'proc' is the process itself. What you want 
is the string that it output, which, in your code, is 'stdout', so:

     return stdout.strip().decode("utf-8")




More information about the Python-list mailing list