Mar-13-2017, 02:27 PM
Sometimes there is problem when you try to call command that is "built-in" in shell (does not exist as an executable file), it could be done with using shell=True parameter. Morever with shell=True its easier to use parameters or wild cards - your entire string is executed in shell/cmd (so its considered less secure).
Output:In [2]: result = subprocess.check_output("ping -c 2 www.google.com", shell=True)
In [3]: print(result)
b'PING www.google.com (172.217.23.228) 56(84) bytes of data.\n64 bytes from prg03s06-in-f4.1e100.net (172.217.23.228): icmp_seq=1 ttl=54 time=10.3 ms\n64 bytes from prg03s06-in-f4.1e100.net (172.217.23.228): icmp_seq=2 ttl=54 time=8.64 ms\n\n--- www.google.com ping statistics ---\n2 packets transmitted, 2 received, 0% packet loss, time 1001ms\nrtt min/avg/max/mdev = 8.648/9.483/10.318/0.835 ms\n'
check_output is nice for "quick" calls - its not so flexible, but if you are interested only in a final result of some program, its useful. Parameter shell=True works with Popen/call too.