Milos Radosavljevic Milos Radosavljevic - 5 days ago 5
Python Question

Added characters to string when exiting subprocces

I am calling a subprocess and returning a string if there is an error.
Code example:

When calling the process:

def read_plan_with_break():
comand = " python script.py "
proc = subprocess.Popen(comand.split(), shell=False, stdout = subprocess.PIPE, stderr= subprocess.PIPE)
if proc.wait() != 0:
output, err = proc.communicate()
print (err)
return "Error in subprocess"
return True


when exiting the subprocess:

def fatal_error():
print("Some message", file=sys.stderr)
exit(1)


My problem is that the
stderr
output is :
b'Some message\r\n'

I can erase the
\r\n
with
strip
but have no idea why there is a
b
at the beginning and the
'
at the start and the end.

Does anyone know why this occurs?

EDIT:
I have tried
err.split()[2:-1]
to get rid of the
b'
but it cuts off the start of the
Some message


If I get a down-vote, please explain so I can improve and make better questions in the future

Answer

err is a bytestring, you should decode it first by err.decode(), this returns the string

Comments