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

srinivasan srinivasan.rns at gmail.com
Sun Nov 25 13:37:45 EST 2018


Dear Mrab,

Even with  "return stdout.strip().decode("utf-8")", it still seems to be an
issue, I am using python 3.6, is this causing a issue?

/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/venv/bin/python
/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py
printing stdout!!!!!!!!!!
printing retcode!!!!!!!!!! 0
Traceback (most recent call last):
  File
"/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py",
line 37, in <module>
    main("Apartment 18", "40672958689850014685")
  File
"/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py",
line 34, in main
    return stdout.strip().decode("utf-8")
AttributeError: 'str' object has no attribute 'decode'

Process finished with exit code 1

Many Thanks in advance


On Sun, Nov 25, 2018 at 11:49 PM MRAB <python at mrabarnett.plus.com> wrote:

> 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")
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list