如何使批处理文件在各自的窗口中依次执行多个(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
注意:否-脚本之间.在这种情况下,您需要修改代码:
>设定结束= 2
>和rest = sys.argv [end:](注意:否1)
标签:cmd,windows,python,batch-file