Pro Girl
Pro Girl

Reputation: 952

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?

Upvotes: 1

Views: 227

Answers (1)

brazosFX
brazosFX

Reputation: 342

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

Upvotes: 1

Related Questions