[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()
您可以使用
start
命令打开一个新窗口(在后台使用/min
)并运行一个命令最后一个命令,pause可以是任何东西,只需确保将其全部加上引号
要使窗口保持打开状态,应将内容放在批处理脚本中,最后一个语句是pause
如果您只有两个脚本,那么您的想法是正确的,只是语法错误:
如果需要窗口标题:
^{pr2}$任何超过两个脚本,你将不得不求助于更棘手的东西。下面的.cmd文件将完成此操作:
这可以扩展到任意数量的脚本或命令,对脚本设置任意参数等。如果您希望cmd窗口在您按下某个键时暂停并消失:
如果您希望它们在最后一个窗口中按一个键立即终止:
所以,对于脚本的行为有很多选择。在
它支持尽可能多的脚本及其参数,您可以在命令行上提供这些脚本的参数。在
如果不为脚本使用参数,则可以简化用法:
^{pr2}$注意:脚本之间没有
。在这种情况下,您需要修改代码:
end = 2
rest = sys.argv[end:]
(注:否+1
)相关问题 更多 >
编程相关推荐