Milos Radosavljevic Milos Radosavljevic - 1 year ago 68
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 "
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)

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

I can erase the
but have no idea why there is a
at the beginning and the
at the start and the end.

Does anyone know why this occurs?

I have tried
to get rid of the
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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download