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


Messages In This Thread
[subprocess]>Run a cmd command and get output into a variable - by CSA75 - Mar-13-2017, 01:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to run shell command, capture the output, then write it into textfile? tatahuft 4 1,142 Dec-20-2024, 02:13 PM
Last Post: Axel_Erfurt
  Using subprocess to execute complex command with many arguments medatib531 5 9,864 Apr-27-2023, 02:23 PM
Last Post: medatib531
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 2,285 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  merge two csv files into output.csv using Subprocess mg24 9 3,841 Dec-11-2022, 09:58 PM
Last Post: Larz60+
  How to combine two output in one variable? ilknurg 2 2,065 Aug-01-2022, 09:31 AM
Last Post: Gribouillis
  How to write a part of powershell command as a variable? ilknurg 2 2,019 Jul-26-2022, 11:31 AM
Last Post: ilknurg
  Pass variable to subprocess paulo79 4 13,096 Apr-12-2022, 12:35 PM
Last Post: DeaD_EyE
  Os command output in variable shows wrong value paulo79 2 2,414 Apr-09-2022, 03:48 PM
Last Post: ndc85430
  How to use a variable in linux command in python code? ilknurg 2 2,521 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  use subprocess on linux\pi wwith a "grep " command korenron 2 11,526 Oct-19-2021, 10:52 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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