2

Can someone tell me how to encode the return statement so that it can decode it. Or whats needs to be changed to get encoded value.

Code

def run_process(cmd_args):
    with subprocess.Popen(cmd_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
        return proc.communicate()


res = run_process(cmd_args);
print(res)
print(res.decode("utf-8"))

Output

print(res.decode("utf-8"))
AttributeError: 'tuple' object has no attribute 'decode'

2 Answers 2

1

Popen.communicate returns a tuple of (stdout, stderr), so you need to treat the returning value as such:

stdout, stderr = run_process(cmd_args);
print(stdout)
print(stdout.decode("utf-8"))

Please read Popen.communicate's documentation for more details.

Sign up to request clarification or add additional context in comments.

1 Comment

Got it ! @blhsing , Thank you so much stdout, stderr = run_process(cmd_args); this helped me.
0

instead of return proc.communicate(),return proc.communicate()[0]. As proc.communicate() is a list of output and error and it seems u only need output part

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.