Anto
Anto

Reputation: 3916

python3 subprocess output enclosed in b' \n'

I am trying to use subprocess module in python3 to fetch output of shell command in MacOS.

command I am using:

read_key = ["binary", "arg1", "arg2", "arg3"]
proc = subprocess.Popen(read_key, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Different output I got.

>>> proc.communicate()
(b'MY_EXPECTED_OUTPUT_STRING\n', b'')

>>> proc.communicate()[0]
b'MY_EXPECTED_OUTPUT_STRING\n'

>>> proc.communicate()[0].strip()
b'MY_EXPECTED_OUTPUT_STRING'
>>>

But I am trying to get "MY_EXPECTED_OUTPUT_STRING" into a variable (without quotes). Is there any proper usage of subprocess which will allow that? Or should I be using truncate, grep etc to get around this issue?

expectation:

print(output)
    >>>>  MY_EXPECTED_OUTPUT_STRING

Am I doing this wrong way? Also help me with simplest, but detailed article on subprocess module which you have bookmarked :)

Upvotes: 0

Views: 697

Answers (3)

Anto
Anto

Reputation: 3916

thanks to @AKH and AnhPC03

I am using below code now:

read_key = ["binary", "arg1", "arg2", "arg3"]
proc = subprocess.Popen(read_key, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8')
output = proc.communicate()[0].strip()
print(output)
MY_EXPECTED_OUTPUT_STRING

Upvotes: 0

AKX
AKX

Reputation: 169032

By default, subprocess.Popen treats the output streams as binary (so you get bytes, hence b'...'). (The subprocess could print out e.g. an image, which is binary data, and you wouldn't want that to be mangled.)

You can add the encoding parameter to have subprocess.Popen decode everything for you:

proc = subprocess.Popen(
    read_key,
    shell=False,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    encoding="utf-8",
)

UTF-8 is generally a safe default unless you know better about your subprocess.

Upvotes: 2

AnhPC03
AnhPC03

Reputation: 646

Your output was in bytes format, you can decode it into utf-8

proc.communicate()[0].strip().decode('utf-8')

Upvotes: 2

Related Questions