Skip to content
Advertisement

How to get exit code when using Python subprocess communicate method?

How do I retrieve the exit code when using Python’s subprocess module and the communicate() method?

Relevant code:

JavaScript

Should I be doing this another way?

Advertisement

Answer

Popen.communicate will set the returncode attribute when it’s done(*). Here’s the relevant documentation section:

JavaScript

So you can just do (I didn’t test it but it should work):

JavaScript

(*) This happens because of the way it’s implemented: after setting up threads to read the child’s streams, it just calls wait.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement