subprocess.Popen在不同的控制台(subprocess.Popen in differ

2019-08-20 21:59发布

我希望这不是一个重复。

我试图使用subprocess.Popen()在一个单独的控制台来打开脚本。 我试过设置的shell=True参数,但没有做的伎俩。

我使用的是32位的Python 2.7在64位的Windows 7。

Answer 1:

from subprocess import *

c = 'dir' #Windows

handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE, shell=True)
print handle.stdout.read()
handle.flush()

如果不使用shell=True你必须提供Popen()一个列表,而不是一个命令字符串,例如:

c = ['ls', '-l'] #Linux

然后再打开无壳。

handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE)
print handle.stdout.read()
handle.flush()

这是你可以调用从Python的一个子进程的最原始的手工和灵活的方式。 如果你只是想输出,去为:

from subproccess import check_output
print check_output('dir')

要打开一个新的控制台GUI窗口,执行X:

import os
os.system("start cmd /K dir") #/K remains the window, /C executes and dies (popup)


Answer 2:

要打开在不同的控制台中,执行(Win7上/ Python 3的测试):

from subprocess import Popen, CREATE_NEW_CONSOLE

Popen('cmd', creationflags=CREATE_NEW_CONSOLE)

input('Enter to exit from Python script...')

有关

我怎样才能创造新的炮弹从基python脚本运行Python脚本?



Answer 3:

我试图creationflags = CREATE_NEW_CONSOLE上的Win7 / Python 2.7版和工作过。



Answer 4:

在Linux的shell =真会做的伎俩:

command = 'python someFile.py' subprocess.Popen('xterm -hold -e "%s"' % command)

这里描述的不使用gnome-终端工作:

https://bbs.archlinux.org/viewtopic.php?id=180103



文章来源: subprocess.Popen in different console