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 :)
CodePudding user response:
Your output was in bytes
format, you can decode it into utf-8
proc.communicate()[0].strip().decode('utf-8')
CodePudding user response:
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.
CodePudding user response:
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]
print(output)
MY_EXPECTED_OUTPUT_STRING