Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
deleted 13 characters in body; edited title
Source Link
user3064538
user3064538

Convert bytes to a string in pythonPython 3

I captured the standard output of an external program into a bytes object:

>>> from subprocess import *
>>> command_stdoutstdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0]
>>>
>>> command_stdoutstdout
b'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2\n'

I want to convert that to a normal Python string, so that I can print it like this:

>>> print(command_stdoutstdout)
-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1
-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2

How do I convert the bytes object to a str with Python 3?


See How to convert string to bytes in Python 3 for the other way around.

Convert bytes to a string in python 3

I captured the standard output of an external program into a bytes object:

>>> from subprocess import *
>>> command_stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0]
>>>
>>> command_stdout
b'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2\n'

I want to convert that to a normal Python string, so that I can print it like this:

>>> print(command_stdout)
-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1
-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2

How do I convert the bytes object to a str with Python 3?


See How to convert string to bytes in Python 3 for the other way around.

Convert bytes to a string in Python 3

I captured the standard output of an external program into a bytes object:

>>> from subprocess import *
>>> stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0]
>>> stdout
b'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2\n'

I want to convert that to a normal Python string, so that I can print it like this:

>>> print(stdout)
-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1
-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2

How do I convert the bytes object to a str with Python 3?


See How to convert string to bytes in Python 3 for the other way around.

edited title
Link
user51

Convert bytes to a string in python 3

added 91 characters in body
Source Link
Karl Knechtel

I captured the standard output of an external program into a bytes object:

>>> from subprocess import *
>>> command_stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0]
>>>
>>> command_stdout
b'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2\n'

I want to convert that to a normal Python string, so that I can print it like this:

>>> print(command_stdout)
-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1
-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2

How do I convert the bytes object to a str with Python 3 3?


See How to convert string to bytes in Python 3 for the other way around.

I captured the standard output of an external program into a bytes object:

>>> from subprocess import *
>>> command_stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0]
>>>
>>> command_stdout
b'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2\n'

I want to convert that to a normal Python string, so that I can print it like this:

>>> print(command_stdout)
-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1
-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2

How do I convert the bytes object to a str with Python 3?

I captured the standard output of an external program into a bytes object:

>>> from subprocess import *
>>> command_stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0]
>>>
>>> command_stdout
b'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2\n'

I want to convert that to a normal Python string, so that I can print it like this:

>>> print(command_stdout)
-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1
-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2

How do I convert the bytes object to a str with Python 3?


See How to convert string to bytes in Python 3 for the other way around.

removed dead end non-solution
Source Link
dumbass
Loading
Fixed the weird syntax highlighting (as a result, the diff looks more extensive than it really is - use view "Side-by-side Markdown" to compare) - an alternative would be to remove the ">>>" and separate the python code from the output.
Source Link
Peter Mortensen
Loading
Accuracy.
Source Link
Mateen Ulhaq
Loading
Shorten a bit more. Most of this can probably be shortened even further since converting to bytes is a general problem, whereas this specific usage of process/stdout/etc does not apply most Googlers.
Source Link
Mateen Ulhaq
Loading
Format. Shorten.
Source Link
Mateen Ulhaq
Loading
Rollback to Revision 6 - Edit approval overridden by post owner or moderator
Source Link
Zoe - Save the data dump
Loading
Javad
Loading
Active reading.
Source Link
Peter Mortensen
Loading
Edited question title and removed language tag.
Link
martineau
Loading
Question Protected by Moinuddin Quadri
Minor copy edit
Source Link
TylerH
Loading
deleted 8 characters in body; edited tags; edited title
Source Link
Martijn Pieters
Loading
added python3.0 tag
Link
jfs
Loading
Source Link
Tomas Sedovic
Loading
lang-py