Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 子流程打开PowerShell,运行命令,然后终止_Python_Python 2.7_Powershell_Subprocess - Fatal编程技术网

Python 子流程打开PowerShell,运行命令,然后终止

Python 子流程打开PowerShell,运行命令,然后终止,python,python-2.7,powershell,subprocess,Python,Python 2.7,Powershell,Subprocess,我编写了一些代码,使用Python子流程模块打开PowerShell窗口,然后在同一窗口中运行命令。PS窗口打开,然后几乎立即关闭。如果我从cmd中删除第二项,下面的代码将打开一个PS窗口并保持打开状态 导入子流程 cmd=['powershell','ls'] prompt=subprocess.Popen(cmd,stdin=subprocess.PIPE) 有什么理由不使用替代品吗?我认为它会完全满足您的要求。这是因为您忘记了与流程进行通信 只需添加一行 output, error = p

我编写了一些代码,使用Python
子流程
模块打开PowerShell窗口,然后在同一窗口中运行命令。PS窗口打开,然后几乎立即关闭。如果我从
cmd
中删除第二项,下面的代码将打开一个PS窗口并保持打开状态

导入子流程
cmd=['powershell','ls']
prompt=subprocess.Popen(cmd,stdin=subprocess.PIPE)

有什么理由不使用替代品吗?我认为它会完全满足您的要求。

这是因为您忘记了与流程进行
通信
只需添加一行

output, error = prompt.communicate()  # this is to start the process
print(output) # add stdout=subprocess.PIPE
print(error)  # add stderr=subprocess.PIPE

PS:我无法帮助您使用powershell,因为我不知道powershell

添加
-noexit
参数如下(
-noprofile
不是必需的):

结果


谢谢!这把它修好了。
import subprocess
cmd = ['powershell', '-noprofile', '-noexit', '&', 'ls *.csv']
prompt = subprocess.call ( cmd )