(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