我希望这不是复制品。
我正在尝试使用subprocess.Popen()
在单独的控制台中打开一个脚本。我尝试过设置shell=True
参数,但没有成功。
我在64位的Windows7上使用32位的Python 2.7。
发布于 2013-04-09 18:44:05
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)
发布于 2013-12-16 21:49:02
要在不同的控制台中打开,请执行以下操作(在Win7 /Python3上测试):
from subprocess import Popen, CREATE_NEW_CONSOLE
Popen('cmd', creationflags=CREATE_NEW_CONSOLE)
input('Enter to exit from Python script...')
相关
How can I spawn new shells to run python scripts from a base python script?
发布于 2016-08-12 03:22:20
在Linux上,shell=True可以做到这一点:
command = 'python someFile.py' subprocess.Popen('xterm -hold -e "%s"' % command)
不能像下面描述的那样使用gnome终端:
https://stackoverflow.com/questions/15899798
复制相似问题