Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Text mode popen
#2
(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
Reply


Messages In This Thread
Text mode popen - by adam2020 - Mar-18-2019, 06:05 PM
RE: Text mode popen - by snippsat - Mar-18-2019, 06:32 PM
RE: Text mode popen - by adam2020 - Mar-18-2019, 06:37 PM
RE: Text mode popen - by snippsat - Mar-18-2019, 06:46 PM
RE: Text mode popen - by adam2020 - Mar-18-2019, 07:35 PM
RE: Text mode popen - by snippsat - Mar-18-2019, 11:12 PM
RE: Text mode popen - by adam2020 - Mar-19-2019, 07:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  things that work in terminal mode but not in sublime mode alok 4 2,940 Aug-11-2021, 07:02 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020