import subprocess
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
process.wait()
print process.returncode
該命令變量應該是例如:cmd /k
。您還可以添加一個stdin=subprocess.PIPE
到POPEN參數列表和寫入命令CMD: subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,stdin=subprocess.PIPE)
最終代碼:
import subprocess
process = subprocess.Popen('cmd /k ', shell=True, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=None)
process.stdin.write("dir") #passing command
stdOutput,stdError = process.communicate()
print stdOutput
process.stdin.close()
或者:
from subprocess import *
Popen("cmd /k dir")
看看python subprocess lib。去谷歌上查詢。 – pregmatch
你需要保持它打開嗎?這是[XY問題](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)?你真的需要實現什麼? –
@PeterWood是的我需要殼打開它只是使用腳本來激活我的virtualenv – Zion