Jun-21-2019, 06:32 PM
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?
encoding=None
setting to encoding='utf-8'
stdout will by string(Unicode default) Python 3.run()
to which should be used in all cases it can handle.subprocess.run() Wrote:The recommended approach to invoking subprocesses is to use therun()
function for all use cases it can handle.
For more advanced use cases, the underlying Popen interface can be used directly.
import subprocess output = subprocess.run(['ping', '-n', '4', 'google.com'], encoding='utf-8', capture_output=True) print(output.stdout)
Output:Pinging google.com [2a00:1450:400f:80b::200e] with 32 bytes of data:
Reply from 2a00:1450:400f:80b::200e: time=42ms
Reply from 2a00:1450:400f:80b::200e: time=33ms
Reply from 2a00:1450:400f:80b::200e: time=46ms
Reply from 2a00:1450:400f:80b::200e: time=50ms
Ping statistics for 2a00:1450:400f:80b::200e:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 33ms, Maximum = 50ms, Average = 42ms
-n 4
on the command and get your pings out as they arrive.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()
.Output:Traceback (most recent call last):
File "test1.py", line 10, in <module>
out = run(['aws','s3','ls'],encoding='utf-8',capture_output=True)
File "/usr/lib/python3.5/subprocess.py", line 693, in run
with Popen(*popenargs, **kwargs) as process:
TypeError: __init__() got an unexpected keyword argument 'encoding'
Output:lt2a/forums /home/forums 8> cat -n /usr/lib/python3.5/subprocess.py|lines 830 874
830 class Popen(object):
831
832 _child_created = False # Set here since __del__ checks it
833
834 def __init__(self, args, bufsize=-1, executable=None,
835 stdin=None, stdout=None, stderr=None,
836 preexec_fn=None, close_fds=_PLATFORM_DEFAULT_CLOSE_FDS,
837 shell=False, cwd=None, env=None, universal_newlines=False,
838 startupinfo=None, creationflags=0,
839 restore_signals=True, start_new_session=False,
840 pass_fds=()):
841 """Create new Popen instance."""
842 _cleanup()
843 # Held while anything is calling waitpid before returncode has been
844 # updated to prevent clobbering returncode if wait() or poll() are
845 # called from multiple threads at once. After acquiring the lock,
846 # code must re-check self.returncode to see if another thread just
847 # finished a waitpid() call.
848 self._waitpid_lock = threading.Lock()
849
850 self._input = None
851 self._communication_started = False
852 if bufsize is None:
853 bufsize = -1 # Restore default
854 if not isinstance(bufsize, int):
855 raise TypeError("bufsize must be an integer")
856
857 if _mswindows:
858 if preexec_fn is not None:
859 raise ValueError("preexec_fn is not supported on Windows "
860 "platforms")
861 any_stdio_set = (stdin is not None or stdout is not None or
862 stderr is not None)
863 if close_fds is _PLATFORM_DEFAULT_CLOSE_FDS:
864 if any_stdio_set:
865 close_fds = False
866 else:
867 close_fds = True
868 elif close_fds and any_stdio_set:
869 raise ValueError(
870 "close_fds is not supported on Windows platforms"
871 " if you redirect stdin/stdout/stderr")
872 else:
873 # POSIX
lt2a/forums /home/forums 9>
(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.
decode()
on stdout output.run()
can capture stdout when given stdout=PIPE
but that capture is bytes and i still need str.