no coding= option fo subprocess.Popen() - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: no coding= option fo subprocess.Popen() (/thread-19291.html) |
no coding= option fo subprocess.Popen() - Skaperen - Jun-21-2019 i want to have utf8 decoding on the stdout pipe with subprocess.Popen() so when it is read i get str instead of bytes, but i see no option to set that. any suggestions? RE: no coding= option fo subprocess.Popen() - snippsat - Jun-21-2019 Have you looked at doc? See parameter encoding=None setting to encoding='utf-8' stdout will by string(Unicode default) Python 3.Work in run() to which should be used in all cases it can handle.subprocess.run() Wrote:The recommended approach to invoking subprocesses is to use the import subprocess output = subprocess.run(['ping', '-n', '4', 'google.com'], encoding='utf-8', capture_output=True) print(output.stdout)
RE: no coding= option fo subprocess.Popen() - Skaperen - Jun-21-2019 i need to read the data as it arrives, hence the need for the pipe. it had previously written the data to a temporary file and read the file and that got str from read, but now i need to read from a pipe, and now i get bytes. i will look at another possible design, but i do want to solve this with Popen(). on your ping command test, try doing it without the option -n 4 on the command and get your pings out as they arrive.i just did try run() and it looks like (from the trace dump) that it calls Popen() itself, passing all the keyword options. my documentation does not show an encoding= option for either run() or Popen() .
from the source code:
RE: no coding= option fo subprocess.Popen() - snippsat - Jun-21-2019 (Jun-21-2019, 08:07 PM)Skaperen Wrote: my documentation does not show an encoding= option for either run() or Popen().Upgrade your Python version,what show here don't work for Python 3.5. In Python 3.5 has to use decode() on stdout output.
RE: no coding= option fo subprocess.Popen() - Skaperen - Jun-21-2019 i found that run() can capture stdout when given stdout=PIPE but that capture is bytes and i still need str.i'm still trying to find a way to upgrade it. pyenv doesn't do it. a function to convert bytes to str would solve this. oh ... bytes.decode(). RE: no coding= option fo subprocess.Popen() - Skaperen - Jun-21-2019 i got a new design working that could handle the output all at once. another coming program will need to do that ... it can handle all the output all at once ... from 16 concurrent processes. it needs to make sure the outputs don't get mixed up, although it can take the 16 outputs in any order. |