如何使批处理文件按顺序执行多个(Python)脚本,每个脚本都在自己的窗口中,并在完成后保持所有这些窗口打开?现在,我的批次是这样的:
python script1
start python script2
pause/cmd
但只有父窗口保持打开状态。
感谢。
环境: Windows XP / Vista
答案 0 :(得分:2)
如果你只有两个脚本,那么你就有了正确的想法,只是语法错误了:
start cmd.exe /k "python script1.py & start cmd.exe /k python script2.py"
如果您需要窗口标题:
start "Window1" cmd.exe /K "python script1.py & start "window2" cmd.exe /K python script2.py"
任何超过两个脚本,你将不得不求助于更棘手的东西。以下.cmd文件可以解决这个问题:
@echo off
if "%~1" == "recurse" goto runScript%~2
start "Window1" cmd /k "%~f0 recurse 1"
exit /b 0
:runScript1
python script1.py
start "Window2" cmd /k "%~f0 recurse 2"
exit /b 0
:runScript2
python script2.py
start "Window3" cmd /k "%~f0 recurse 3"
exit /b 0
:runScript3
python script3.py
exit /b 0
这可以扩展到任意数量的脚本或命令,脚本的任意参数等。如果你想让cmd窗口暂停,当你按一个键时消失:
@echo off
if "%~1" == "recurse" goto runScript%~2
start "Window1" cmd /c "%~f0 recurse 1"
exit /b 0
:runScript1
python script1.py
start "Window2" cmd /c "%~f0 recurse 2"
pause
exit /b 0
:runScript2
python script2.py
start "Window3" cmd /c "%~f0 recurse 3"
pause
exit /b 0
:runScript3
python script3.py
pause
exit /b 0
如果您想让所有人在最后一个窗口按一个键立即终止:
@echo off
if "%~1" == "recurse" goto runScript%~2
start "Window1" cmd /c "%~f0 recurse 1"
exit /b 0
:runScript1
python script1.py
start "Window2" /wait cmd /c "%~f0 recurse 2"
exit /b 0
:runScript2
python script2.py
start "Window3" /wait cmd /c "%~f0 recurse 3"
exit /b 0
:runScript3
python script3.py
pause
exit /b 0
因此,您可以选择许多脚本行为选项。
答案 1 :(得分:2)
[to]按顺序执行多个(Python)脚本,每个脚本都在自己的窗口中,并在完成后保持所有窗口打开
#!/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
)答案 2 :(得分:0)
您可以使用start
命令打开一个新窗口(在后台使用/min
)并运行命令
start "window1" /min cmd.exe /c "pause"
最后一个命令,暂停可以是任何东西,只需确保将它全部放在引号中
要保持窗口打开,您应该将内容放在批处理脚本中,并将暂停作为最后一个语句