python - 批量打开多个控制台窗口

标签 python windows batch-file cmd

如何让批处理文件按顺序执行多个 (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

注意:脚本之间没有--。这种情况下需要修改代码:

  • 设置end = 2
  • rest = sys.argv[end:](注意:没有+1)

关于python - 批量打开多个控制台窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12122535/

相关文章:

java - System.loadLibrary() 错误 - 无法指定相对或绝对路径

excel - 运行某个 xls 文件

python - Django:如何在测试 View 时模拟文件上传

python - pcap 缺少元素?

python - downloadPage 回调函数的问题

batch-file - FOR递归期间如何将文件保存到当前位置

windows - 命令的输入参数在批量编程时需要用户手动输入

python - 如何在python中实现多个构造函数?

c++ - 如何隐藏菜单? lpsz菜单名

Windows 相当于 Unix 上的文件 .netrc?