如何让批处理文件按顺序执行多个 (Python) 脚本,每个脚本都在自己的窗口中,并在完成后保持所有这些窗口打开?现在,我的批处理是这样的:
python script1
start python script2
pause/cmd
但只有父窗口保持打开状态。
谢谢。
环境: Windows XP/Vista
最佳答案
[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()
它支持尽可能多的脚本及其参数,您可以在命令行上提供。
如果您不为脚本使用参数;你可以简化用法:
$ python cps.py script1.py script2.py script3.py
注意:脚本之间没有--
。这种情况下需要修改代码:
- 设置
end = 2
- 和
rest = sys.argv[end:]
(注意:没有+1
)
关于python - 批量打开多个控制台窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12122535/