Home>
I want to achieve

I want to divide the process when it is err as a result of command execution "communicate ()" in subprocess.
Do you feel like using if statements?
Thanks for your support.

* The following example is intentionally set to "err".

import subprocess
p1 = subprocess.Popen (['ping','test'], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
out, err = p1.communicate ()
print (out)
print (err)

### Output result ###
b''
b'ping: test: \ xe5 \ x90 \ x8d \ xe5 \ x89 \ x8d \ xe3 \ x81 \ xbe \ xe3 \ x81 \ x9f \ xe3 \ x81 \ xaf \ xe3 \ x82 \ xb5 \ xe3 \ x83 \ xbc \ xe3 \ x83 \ x93 \ xe3 \ x82 \ xb9 \ xe3 \ x81 \ x8c \ xe4 \ xb8 \ x8d \ xe6 \ x98 \ x8e \ xe3 \ x81 \ xa7 \ xe3 \ x81 \ x99 \ n'
  • Answer # 1

    if p1.returncode>0:Is not it.

Related articles

Trends