Hi guys,
first time poster sorry if my post is in the wrong format,
anyway I'm following the docs -
https://docs.python.org/3/library/subpro...ommunicate
prc.communicate()
returns a tuple of stdout and stderr,
prc = popen(command,stdout = PIPE,stderr = PIPE
so the above code creates a new process I specify that stdout will be PIPE,first off what does stdout = PIPE do and how does it relate the functionality in this case of the function/method prc.communicate()?
secondly I read that this will return the output of the command in bytes but what if I want to get the string of the output and not the bytes? I read that you need to to specify a text mode attribute but how is this done?
thanks
(Mar-18-2019, 06:05 PM)adam2020 Wrote: [ -> ]secondly I read that this will return the output of the command in bytes but what if I want to get the string of the output and not the bytes?
Use
decode()
to go from bytes to string in Python 3.
Also
run() has made it simpler to catch output as it has a parameter
capture_output=True
.
import subprocess
out = subprocess.run(['ping', 'google.com'], capture_output=True)
print(out.stdout.decode()) #Same as decode('utf-8')
Output:
Pinging google.com [216.58.211.142] with 32 bytes of data:
Reply from 216.58.211.142: bytes=32 time=63ms TTL=54
Reply from 216.58.211.142: bytes=32 time=70ms TTL=54
Reply from 216.58.211.142: bytes=32 time=77ms TTL=54
Reply from 216.58.211.142: bytes=32 time=47ms TTL=54
Ping statistics for 216.58.211.142:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 47ms, Maximum = 77ms, Average = 64ms
thanks snippsat great answer :)
what is the main difference
between
sub = subprocess.run()
and
process = popen(['command])(
and when would you choose to use one over the other?
thanks
(Mar-18-2019, 06:37 PM)adam2020 Wrote: [ -> ]and when would you choose to use one over the other?
It's explained in
Using the subprocess Module.
Quote:The recommended approach to invoking subprocesses is to use the run()
function for all use cases it can handle.
For more advanced use cases, the underlying Popen interface can be used directly.
So in my example
run()
can handle getting output from ping command,so then i should use
run()
.
thanks snippsat :)
only issue I am getting is that print(out.stdout.decode()) #Same as decode('utf-8')
the decode() function doesn't seem to be a function of out.stdout.decode() on my implementation, also the capture output param doesn't seem to be available on my implementation neither
I'm using python3
I'm using Python 3.5.2 from what I read the capture_output flag and decode() are only added in 3.7
is there any alternative?
(Mar-18-2019, 07:35 PM)adam2020 Wrote: [ -> ]I'm using Python 3.5.2 from what I read the capture_output flag and decode() are only added in 3.7
Upgrade your Python version.
(Mar-18-2019, 07:35 PM)adam2020 Wrote: [ -> ]is there any alternative?
Sure both Popen and
check_output() can be used.
from subprocess import check_output
# linux
#out = check_output(['ping', '-c', '4', 'google.com'])
out = check_output(['ping', 'google.com'])
print(out.decode())
import subprocess
with subprocess.Popen(['ping', 'google.com'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
out, err = p.communicate()
print(out.decode())
thanks snippsat :)
so many ways :p