osdir.com

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

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


On 11/25/18 10:58 AM, srinivasan wrote:
> Even only with "*proc.decode("utf-8")"* in the above code still it seems to
> throw the error
> 
>     #return proc.strip().decode("utf-8")
>     #return proc.decode("utf-8").strip()
> *    return proc.decode("utf-8")*
> 
> Error:
> /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 31, in <module>
>     main("Apartment 18", "40672958689850014685")
> *  File
> "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py",
> line 29, in main*
> *    return proc.decode("utf-8")*
> *AttributeError: 'Popen' object has no attribute 'decode'*

the error tells you what is wrong.  proc is a Popen object, and does not
have a decode method.  simple enough.  strings have a decode method.
You're decoding the wrong thing, you need to decode what you get back
from communicating with the Popen object, not the Popen object itself.

stdin is the string (or more likely, in Python 3, a bytes object) you
got back from calling communicate(). decode that.