[英]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.我将 Windows10 与 Python3 一起使用,并且我正在使用 subprocess 模块中的 popen() 函数。
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
.我想在新命令窗口中运行的代码在同一个目录中,它被称为
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.请注意,我已经阅读了文章: How to execute a command prompt command from python但无济于事。
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?
如何在该窗口中运行脚本“async.py”?
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?使用子进程成功打开 Python 脚本后,如何在新的终端窗口中运行它?
Try adding /C after cmd and then feeding the command async.尝试在 cmd 之后添加 /C ,然后异步输入命令。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.