Since this question is actually asking about subprocess
output, you have more direct approaches available. The most modern would be using subprocess.check_output
and passing text=True
(Python 3.7+) to automatically decode stdout using the system default coding:
text = subprocess.check_output(["ls", "-l"], text=True)
For Python 3.6, Popen
accepts an encoding keyword:
>>> from subprocess import Popen, PIPE
>>> text = Popen(['ls', '-l'], stdout=PIPE, encoding='utf-8').communicate()[0]
>>> type(text)
str
>>> print(text)
total 0
-rw-r--r-- 1 wim badger 0 May 31 12:45 some_file.txt
The general answer to the question in the title, if you're not dealing with subprocess output, is to decode bytes to text:
>>> b'abcde'.decode()
'abcde'
With no argument, sys.getdefaultencoding()
will be used. If your data is not sys.getdefaultencoding()
, then you must specify the encoding explicitly in the decode
call:
>>> b'caf\xe9'.decode('cp1250')
'café'