简体   繁体   English

打开新的cmd窗口后,如何从python3批处理文件在终端中推送命令?

[英]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.

相关问题 如何使用Python控制从.cmd文件打开的命令窗口 - How to control a command window opened from a .cmd file using Python 在新的终端窗口中从 python 执行终端命令? - Execute terminal command from python in new terminal window? Python:如何在新的终端窗口/命令提示符下执行线程? - Python: How to execute a thread in a new terminal window/command prompt? 如何在python中编辑url并打开新页面而不打开新窗口或标签? - How do I edit the url in python and open a new page without having a new window or tab opened? Python:如何在新的cmd窗口中执行bat文件? - Python: How can I execute bat file in new cmd window? Python3安装成功,但无法在终端中打开 - Python3 installed successfully, but cannot be opened in terminal MacOS 终端命令“python3 file.py”进入目录而不是从 PATH 执行脚本文件 - MacOS terminal command 'python3 file.py' goes into directory instead of executing script file from PATH 在终端中打开另一个文件后如何清理终端(cmd) - How to clean terminal(cmd) , after opening another file in terminal 尝试从批处理文件运行 python 脚本,而不会弹出 cmd window - Trying to run a python script from a batch file without the cmd window popping up python3从命令终端获取参数并打印输出 - python3 get parameter from command terminal and print input out
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM