如何从 Python 启动命令窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12746130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to launch a command window from Python
提问by Dan H
I'd like to use Python 2.6 on Windows to launch several separatecommand windows, each running their own Python script. The purpose is: these are clients, and I'm trying to load up the server with requests from multiple quasi-independent clients.
我想在 Windows 上使用 Python 2.6 来启动几个单独的命令窗口,每个窗口都运行自己的 Python 脚本。目的是:这些是客户端,我正在尝试使用来自多个准独立客户端的请求加载服务器。
I don't need to communicate with the client during or after the run, but I do need to send each a different commmandline arg, and I'd like each client's output to scroll in its own "console".
我不需要在运行期间或运行后与客户端通信,但我确实需要向每个客户端发送一个不同的命令行参数,并且我希望每个客户端的输出在其自己的“控制台”中滚动。
From the DOS command line, the "start" command does what I'd like. I can either:
在 DOS 命令行中,“start”命令执行我想要的操作。我可以:
start perf_test.py 2
or
或者
start cmd /c perf_test.py 3
or
或者
start cmd /c python perf_test.py 4
(These will work for you if you have your "file associations" setup correctly for *.py files. There are other threads on that, if you need help. Or, use full paths to the python exe and/or your script.)
(如果您为 *.py 文件正确设置了“文件关联”,这些将对您有用。如果您需要帮助,还有其他线程。或者,使用 python exe 和/或脚本的完整路径。)
My challenge is: How do I get the same effect from Python?
我的挑战是:如何从 Python 获得相同的效果?
Using subprocesslibrary, I've tried variations like this:
使用subprocess库,我尝试过这样的变体:
from subprocess import *
p = Popen(["perf_test.py", "4"], shell=True, stdin=PIPE)
But even with shell=True, the output is commingled in the window I'm already running in. Adding stdout=PIPEstops that, but then I have to read p.stdoutor use p.communicate(). Adding "cmd" to the Popen gets approximately the same:
但即使使用shell=True,输出也会混合在我已经运行的窗口中。添加会stdout=PIPE停止,但随后我必须阅读p.stdout或使用p.communicate(). 向 Popen 添加“cmd”大致相同:
p = Popen(["cmd", "/c", "perf_test.py", "4"], shell=True, stdin=PIPE)
None of the above achieve the effect I'm looking for, which is: "pop open a new, distinct window for this script, and watch its output scroll by in its own console" (because I really want to run N of these clients in parallel).
以上都没有达到我想要的效果,即:“为这个脚本弹出一个新的、不同的窗口,并在它自己的控制台中观察它的输出滚动”(因为我真的想运行 N 个这些客户端在平行下)。
One other thing I turned to almostworks, too.
我转向的另一件事也几乎有效。
import os
os.startfile("perf_test.py")
This returns immediately, and an actual dosbox pops up. Yay! Success! That is, until I try to add an argument. This fails:
这会立即返回,并弹出一个实际的 dosbox。好极了!成功!也就是说,直到我尝试添加一个论点。这失败了:
os.startfile("perf_test.py 5")
with error "The system cannot find the file specified"... because it is adding "[SPACE]5" to the filename. (The purpose of the argument is that each "perf_test" needs to have an assigned ID, so that they hit the server as different instances.)
出现错误“系统找不到指定的文件”...因为它在文件名中添加了“[SPACE]5”。(参数的目的是每个“perf_test”都需要分配一个 ID,以便它们作为不同的实例访问服务器。)
Other approaches I've considered, and really don't like for various reasons:
我考虑过的其他方法,由于各种原因真的不喜欢:
- Run each "perf_test" in its own thread. (But I really want to see the output each in its own console.)
- Make my own pseudo-consoles with Tk. (Figure I'll just hit different threading problems there.)
- Dynamically write a .BAT file on the fly with the lines "start perf_test.py 1", "start perf_test.py 2", etc., then launch that .BAT file with
Popenorstartfile.
- 在自己的线程中运行每个“perf_test”。(但我真的很想在自己的控制台中看到每个输出。)
- 用 Tk 制作我自己的伪控制台。(图我只会在那里遇到不同的线程问题。)
- 使用“start perf_test.py 1”、“start perf_test.py 2”等行动态编写 .BAT 文件,然后使用
Popen或启动该 .BAT 文件startfile。
I expect the last will work... and I guess is my last resort, if I can't get a Python script to do it directly.
我希望最后一个会起作用……如果我无法直接获得 Python 脚本,我想这是我最后的手段。
Thanks for any input / insights!
感谢您的任何输入/见解!
采纳答案by Rod
You can use:
您可以使用:
import os
os.system("start python perf_test.py 5")
回答by assassin44037
Code :
代码 :
user = raw_input("welcome to cmd: ")
def print_perms(chars, minlen, maxlen):
for n in range(minlen, maxlen+1):
for perm in itertools.product(chars, repeat=n):
print(''.join(perm))
回答by PGC channel
Well I had the same problem. It was solved with this method. You should Try:
好吧,我遇到了同样的问题。用这个方法解决了。你应该试试:
import os
os.system('chrome.exe') # open Chrome (.exe file)
回答by Garvit Joshi
import os
os.system("start cmd")

