3

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.

thanks.

Environment: Windows XP/Vista

4

3 回答 3

2

If you have only two scripts, you had the right idea, just got your syntax wrong:

start cmd.exe /k "python script1.py & start cmd.exe /k python script2.py"

If you need window titles:

start "Window1" cmd.exe /K "python script1.py & start "window2" cmd.exe /K python script2.py"

Any more than two scripts, and you will have to resort to trickier stuff. The following .cmd file will do the trick:

@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

And this is scalable to any number of scripts or commands, with arbitrary parameters to the scripts, etc. If you want the cmd windows to just pause, and disappear when you press a key:

@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

If you want them all to terminate instantly at the press of one key on the final window:

@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

So, you have lots of options for behaviour of the script.

于 2012-08-26T22:48:45.100 回答
2

[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:

  • set end = 2
  • and rest = sys.argv[end:] (Note: no +1)
于 2012-08-27T14:39:47.927 回答
0

You can use the start command to open a new window (in the background with /min) and run a command

start "window1" /min cmd.exe /c "pause"

The final command, pause can be anything, just be sure to put it all in quotes

To keep a window open, you should place the content in a batch script with a pause as the last statement

于 2012-08-25T13:53:02.977 回答