如何通过python subprocess.popen在Windows中打开的命令窗口中执行命令?

问题描述 投票:0回答:1

我想通过subprocess.pOpen命令打开的命令窗口中执行多个命令?怎么做?

类似

p = subprocess.Popen("start cmd /k ", stdin=subprocess.PIPE, stdout=subprocess.PIPE,shell=True )

现在,我想在打开的同一窗口中调用多个命令。我不想将参数作为列表传递,因为它将仅被视为列表中第一个元素的参数。

python subprocess popen
1个回答
0
投票

下面是我总是方便地运行Windows命令的代码段。

import subprocess
result = []
win_cmd = 'ipconfig'
#shell=True means do not show the command shell
#shell=False means show the command shell
process = subprocess.Popen(win_cmd,
            shell=False,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE )
for line in process.stdout:
    print (line)
result.append(line)
errcode = process.returncode
for line in result:
    print (line)
© www.soinside.com 2019 - 2024. All rights reserved.