Python Forum

Full Version: [subprocess]>Run a cmd command and get output into a variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello All,

I'd like to run a windows cmd and get the result in a variable..it works well for a command but not for another... Confused 

Here is :

Command "whoami" > Works !!  Smile


import subprocess
p1=subprocess.Popen(["whoami"],stdout=subprocess.PIPE)
print(p1.communicate()[0])


Output:
b'asus\\joe\r\n'
Command "path" > Doesn't Works !! Confused  


import subprocess
p1=subprocess.Popen(["path"],stdout=subprocess.PIPE)
print(p1.communicate()[0])


Output:
Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "C:\Users\Joe\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 676, in __init__     restore_signals, start_new_session)   File "C:\Users\Joe\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 955, in _execute_child     startupinfo) FileNotFoundError: [WinError 2] [color=#545454][size=small][font=arial, sans-serif]The system cannot find the file specified[/font][/size][/color]
The command "path" is just an example, in fact I'd like to execute a more complex command but it causes python to the same error...

thanks for all :-)
output = subprocess.call(['path'])
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.
(Mar-13-2017, 02:27 PM)zivoni Wrote: [ -> ]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.
This is perfect !! Thank you !
PATH is an environment variable, you can get the value with:

import os
path=os.environ['PATH']