Interacting with cmd prompt during loop - python

I'm trying to open the command prompt in a loop from a Python script, and within the command prompt 1.) type to change directory and 2.) type a command. Once the command is entered and running, I'll need to wait for it to finish before moving to the next part of the script, but haven't got that far.
I have the following, which I hope can open cmd and communicate.
import subprocess
process = subprocess.Popen('cmd', stdin=subprocess.PIPE)
process.communicate(b'cd /target/directory')
process.communicate(b'run.bat')
Am I on the right path, and how can I make the script wait until the command has finished then close?
*Edit:
I have a run.bat file within a directory, and would like to run it in the command prompt within its specific directory. I've tried running the .bat file by itself but haven't been successful. I have a cmd shortcut that opens the prompt in the same directory as run.bat, and entering 'run.bat' of course works. As this has been the only thing that worked so far, I'm trying to replicate these steps in a Python script.

To wait for the command to finish then exit you can simply add this instead of the second communicate
process.communicate(b'run.bat && exit 0')
This will run the run.bat file then after it is finished exit with code 0

Related

How to launch python from within python Windows 10)?

I am running python3 in Windows 10. Within my program, I try to launch another python instance.
os.system("python -m http.server")
print("All Done")
or
os.system("python")
I want to see a Window shell that pops up and allow me the see the output and interact with it. But instead the program just continues. I don't even see a window pops up. What am I missing?
If you want to open a new window, you need to use start cmd
If you want to open a new window running a command from cmd, that's start cmd /c "command"
e.g
import os
os.system('start cmd /c "python -m http.server"')
However, if you really just want to run another python process, you can import and run the server directly from code - refer docs

Windows Command Prompt CMD Close

In Python, How can I check and close such cmd windows like in picture? This code should control and find which cmd is finished and idle like in picture, then close it. I don't want a code which is waiting to close it. It should be a detached program. By the way, the image is a sample. It is not my original problem.
You can think that you have a window or several windows opened and waiting like this, how can they be closed? There is a condition to close a command prompt. This condition is that program on that prompt should be done. Command prompts on which a program runs should not be closed. Moreover, this program should intervene externally and afterwards.
I can't solve with taskkill or psutil. They always see the state of cmd as running. More specifically, you can look at code snippet below,
os.system("start /B start cmd.exe #cmd /k python helloworld.py & exit")
It doesn't close the command prompt.
I don't know if you're looking for this solution, but you can add "& exit" after your script, and that will close CMD.
For example:
python script.py & exit

Python and Win10 - run command and repeat when finished

I am currently using os.system('start cmd.exe /k ' + command)
where “command” is a string that opens an exe file with some arguments.
I know I could avoid calling cmd.exe, but to me it’s easier with that.
When the executable stops, cmd doesn’t close and remains idle waiting for another command; what I want to do is: when the execution finishes, make my python script call the same command again in the same cmd window.
EDIT: I am running multiple instances of cmd.exe which open the same exe with different parameters, my aim is to do the same as above for every instance
returncode = os.system(mycommand)
print(returncode)
prints “0” just after running cmd (without waiting for the execution to end)
I don’t really need the exit code, but parsing the text output from the window might be useful to know whether to stop looping the command or just wait some time before running it again

How to push in a command in terminal from a python3 batch file after having opened a new cmd window?

I've written a batch file that opens a new command terminal.
I'm using Windows10 with Python3, and I'm using the popen() function from the subprocess module.
Opening the command window succeeds. However, I can't push/pipe the code that I want the new command window to run.
The code I want to run in the new command window is in the same directory, and it's called async.py.
How can I run that script on the new command window that I have opened?
Kindly note that I have already read the articles:
How to execute a command prompt command from python
but to no avail.
This is my script:
from subprocess import Popen, CREATE_NEW_CONSOLE
Popen('cmd', creationflags=CREATE_NEW_CONSOLE)
the new window opens.
How to run the script "async.py" in that window?
I also tried:
runpy.run_module(mod_name='async')
but it runs the script in the same window.
I also tried:
os.popen('C:\\Users\\Me\\PycharmProjects\\async.py')
which does absolutely nothing.
To recap the question:
How do I run the Python script in the new terminal window after successfully opening it with subprocess?
Try adding /C after cmd and then feeding the command async. Like this...
Popen('cmd /C async.py', creationflags=CREATE_NEW_CONSOLE)
You may need to do full paths. Also if you require a second window to start, you may need to run a command like this:
start c:\windows\system32\cmd.exe /C c:\whatever\location\async.py

what shall I do to kill a running script and go back to the prompt in python

I use Ctrl+C each time to kill a running script and go back to the prompt. But then I lose the prompt, only to get "KeyboardInterrupt".
How should I kill a running script and get the python prompt back?
If you're saying that you want to launch the Python interactive terminal after a script finished running (both normally and by keyboard interrupt), then just launch Python with the -i tag. For example: python -i <script-name> <script-args>

Categories

Resources