.. availability:: Unix.
-.. function:: popen(cmd, mode='r', buffering=-1)
+.. function:: popen(cmd, mode='r', buffering=-1, encoding=None)
Open a pipe to or from command *cmd*.
The return value is an open file object
connected to the pipe, which can be read or written depending on whether *mode*
- is ``'r'`` (default) or ``'w'``. The *buffering* argument has the same meaning as
+ is ``'r'`` (default) or ``'w'``.
+ The *buffering* and *encoding* arguments have the same meaning as
the corresponding argument to the built-in :func:`open` function. The
returned file object reads or writes text strings rather than bytes.
documentation for more powerful ways to manage and communicate with
subprocesses.
+ .. versionchanged:: 3.11
+ Added the *encoding* parameter.
+
.. function:: posix_spawn(path, argv, env, *, file_actions=None, \
setpgroup=None, resetids=False, setsid=False, setsigmask=(), \
# command in a shell can't be supported.
if sys.platform != 'vxworks':
# Supply os.popen()
- def popen(cmd, mode="r", buffering=-1):
+ def popen(cmd, mode="r", buffering=-1, encoding=None):
if not isinstance(cmd, str):
raise TypeError("invalid cmd type (%s, expected string)" % type(cmd))
if mode not in ("r", "w"):
if buffering == 0 or buffering is None:
raise ValueError("popen() does not support unbuffered streams")
import subprocess, io
+ encoding = io.text_encoding(encoding)
if mode == "r":
proc = subprocess.Popen(cmd,
shell=True, text=True,