keep multiple console windows open from batch

后端 未结 3 747
醉话见心
醉话见心 2020-12-06 14:31

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 i

3条回答
  •  悲&欢浪女
    2020-12-06 15:27

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

提交回复
热议问题