Python Forum
[subprocess]>Run a cmd command and get output into a variable
Thread Rating:
  • 3 Vote(s) - 3.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[subprocess]>Run a cmd command and get output into a variable
#1
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 :-)
Reply
#2
output = subprocess.call(['path'])
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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.
Reply
#4
(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 !
Reply
#5
PATH is an environment variable, you can get the value with:

import os
path=os.environ['PATH']
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using subprocess to execute complex command with many arguments medatib531 5 1,841 Apr-27-2023, 02:23 PM
Last Post: medatib531
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,075 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  merge two csv files into output.csv using Subprocess mg24 9 1,756 Dec-11-2022, 09:58 PM
Last Post: Larz60+
  How to combine two output in one variable? ilknurg 2 1,183 Aug-01-2022, 09:31 AM
Last Post: Gribouillis
  How to write a part of powershell command as a variable? ilknurg 2 1,118 Jul-26-2022, 11:31 AM
Last Post: ilknurg
  Pass variable to subprocess paulo79 4 10,062 Apr-12-2022, 12:35 PM
Last Post: DeaD_EyE
  Os command output in variable shows wrong value paulo79 2 1,503 Apr-09-2022, 03:48 PM
Last Post: ndc85430
  How to use a variable in linux command in python code? ilknurg 2 1,595 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  use subprocess on linux\pi wwith a "grep " command korenron 2 8,064 Oct-19-2021, 10:52 AM
Last Post: DeaD_EyE
  Command output to Variable ironclaw 1 1,773 Aug-26-2021, 06:55 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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