批量打开多个控制台窗口 [英] keep multiple console windows open from batch
问题描述
如何使批处理文件依次在其自己的窗口中执行多个(Python)脚本,并在完成后使所有这些窗口保持打开状态?现在,我的批处理是这样的:
How can I make a batch file execute multiple (Python) scripts sequentially, each in their own window, and keep all those windows open upon completion? Right now, my batch is something like:
python script1
start python script2
pause/cmd
但是只有父窗口保持打开状态.
But only the parent window stays open.
谢谢.
环境: Windows XP/Vista
Environment: Windows XP/Vista
推荐答案
[以]依次执行多个(Python)脚本,每个脚本都在各自的窗口中,并在完成时使所有这些窗口保持打开状态
[to] execute multiple (Python) scripts sequentially, each in their own window, and keep all those windows open upon completion
#!/usr/bin/env python
"""Continuation-passing style (CPS) script.
Usage:
$ python cps.py script1.py arg1 arg2 -- script2.py a b c -- script3.py ...
"""
import platform
import sys
from subprocess import call
if len(sys.argv) < 2:
sys.exit() # nothing to do
# define a command that starts new terminal
if platform.system() == "Windows":
new_window_command = "cmd.exe /c start cmd.exe /c".split()
else: #XXX this can be made more portable
new_window_command = "x-terminal-emulator -e".split()
# find where script args end
end = sys.argv.index('--') if '--' in sys.argv else len(sys.argv)
# call script; wait while it ends; ignore errors
call([sys.executable] + sys.argv[1:end])
# start new window; call itself; pass the rest; ignore errors
rest = sys.argv[end+1:]
if rest:
call(new_window_command + [sys.executable, sys.argv[0]] + rest)
print("Press Enter to exit") #NOTE: to avoid raw_input/input py3k shenanigans
sys.stdin.readline()
它支持的脚本数量与您在命令行上提供的数量一样.
It supports as many scripts with their arguments as you can supply on the command line.
如果您不对脚本使用参数;您可以简化用法:
If you don't use arguments for scripts; you could simplify the usage:
$ python cps.py script1.py script2.py script3.py
注意:脚本之间没有--
.在这种情况下,您需要修改代码:
Note: no --
between scripts. You need to modify the code in this case:
- 设置
end = 2
- 和
rest = sys.argv[end:]
(注意:否+1
)
- set
end = 2
- and
rest = sys.argv[end:]
(Note: no+1
)
这篇关于批量打开多个控制台窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!