Hi everyone can anyone elaborate on the difference between
subprocess.Popen
subprocess.call
subprocess.check_output
and also if possible then please explain difference between
x.readlines()
versus x.communicate()
?
i.e difference between
import subprocess
from subprocess import PIPE
ls = subprocess.Popen(['ls','-l'], stdout=subprocess.PIPE)
**out = ls.stdout.readlines()**
print out
and
import subprocess
from subprocess import PIPE
ls = subprocess.Popen(['ls','-l'], stdout=subprocess.PIPE)
out = ls.communicate()
print out
call
and check_output
(along with check_call
) are just utility functions which call Popen
under the hood.
call
returns the exit code of child processcheck_call
raises CalledProcessError
error if exit code was non zerocheck_output
same as above but also returns output.The difference between readlines
and communicate
is that readlines
is simply a function made on the buffer (stdout
) while communicate is a method of process class so it can handle different exceptions, you can pass input in it, and it waits for the process to finish.
Read more here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With