Is possible to manipulate cmd window from python without closing it everytime I send a new command?
I would like to manipulate it as I do with keyboard, command by command, with command prompt opened, so I could see outputs.
For now I'm using this, but is not what I want...
subprocess.Popen('start dir', shell=True)
Related
I'm able to run commands through CMD with Python with subprocess.call('insert command here') but it opens the CMD window then closes right away. Is there a way to not open it at all?
You can set shell=True when calling subprocess.call:
subprocess.call('insert command here', shell=True)
I run Windows 10 and I'm trying to execute linux sommands in CMD.
I have installed ubuntu so that I can run bash in CMD
Now I want to be able to run commands in this CMD window.
In this case a simple ping command.
First. I can not figure how to reuse the CMD window, I don't want to open a new CMD for every loop.
Second. I want to make sure that the command is done before executing the next in the loop.
cmd_shell = subprocess.Popen('start cmd /K bash', shell=True)
sites = ["google.com", "python.org"]
for site in sites:
cmd_line = "ping -c4 "+site
# this is where I need help to execute the ping command in the already opend CMD window
After I have open and executed bash, I cant find a good way to write in the CMD window.
I did a not so nice solution using pyautogui but I think there's a much better way to do it, but I dont know how.
Help please.
/ BR BaconFlip
So I realized that I did not need a CMD window to do this.
I could just run it in the script like this:
sites = ["google.com", "python.org"]
for site in sites:
cmd_line = '"ping -c4 '+site+'"'
command = check_output('c:\\Windows\\System32\\bash.exe -c '+cmd_line)
print(command)
Basically what I'm looking to do is to have python code that I can run from cmd that opens a new cmd window then types commands into it. I have been able to open and run commands but the commands always run in the first cmd window. I think I might need to switch the cursor or something like that, but I don't know how.
Here is my current code.
import subprocess
subprocess.call('start', shell=True)
import time
time.sleep(5)
cmd=['', ''] ### In the second '' you can type in cmd.
for each in cmd:
process=subprocess.Popen(each,stdout=subprocess.PIPE,shell=True)
output=process.communicate()[0]
print(output.decode('utf-8'))
- Thanks
EDIT :
NVM Its in the similar one
I have to execute python script in windows command prompt
I am using the following command to run the command, so that the script opens the command prompt execute it
os.system("start /wait cmd /c {c:\\python27\\python.exe C:\\examples\\xml2html.py --dir c:\\Temp\\abcd c:\\tmp\\results.xml}")
I will be expecting a new directory called "abcd" created at that location and some output files created inside that.
When I run this command normally in the windows command prompt it works. I am not able to execute this in the script. Windows command prompt opens and terminates quickly.
Could any one let me know where exactly is it going wrong with the command please?
Unless you want to open a new console window you don't need to run cmd.exe (%COMSPEC%) in order to run another Python script as a subprocess:
import sys
from subprocess import check_call
check_call([sys.executable, "C:\\examples\\xml2html.py",
"--dir", "c:\\Temp\\abcd", "c:\\tmp\\results.xml"])
In Windows XP when you open cmd.exe you get a console window with a command prompt looking like:
"C:\User and Settings\Staffer\My Documents>" where s> the underscore after the '>' is the cursor.
This is the default for Windows XP. A user might change it using the PROMPT=something or by using set PROMPT=something
In the console window, at the command prompt, entering the internal command "prompt" with no arguments does not return what the current prompt string is.
Is there a command or preferably a Python library that can retrieve what the command prompt is. I didn't want to write a Python module if there was a builtin way of retrieving that string.
The use case for getting the command prompt string is when I use the Python subprocess module to run a python program, and then return to the same console's command prompt while the subprocess is running, I get the cursor on a blank line. I can press Enter and the command prompt will redisplay; but it looks as if hasn't returned from the subprocess yet, which misleads my users.
One solution for the gui part of my app is to run pythonw runapp.py. However I'm left wondering if there's a way to get a clean command prompt when calling subprocess by using already made DOS commands, Python library, proper use of subprocess.Popen() and communicate()?
Not sure if it helps but if you enter "SET" from the command prompt you'll see a list of environment variables, including the current PROMPT (however it won't appear in the list if it's the default prompt).
From the command line:
c:\>echo %prompt%
$P$G
From Python:
>>> import os
>>> os.environ["PROMPT"]
'$P$G'
(http://docs.python.org/library/os.html#process-parameters)
[edit]
Ah, I missed your edit. It sounds like all you want to do is run the script in the background. I believe you are looking for the Windows "start" command with the /b option - http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/start.mspx?mfr=true
I think you are looking for this:
import os
print os.getcwd()