osdir.com

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


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")