Reputation: 55
I've written some code that uses the Python subprocess
module to open a PowerShell window, then run a command in that same window. The PS window opens, then almost immediately closes. The code below will open a PS window and leave it open if I removed the second item from cmd
.
import subprocess
cmd = ['powershell', 'ls']
prompt = subprocess.Popen(cmd, stdin=subprocess.PIPE)
Upvotes: 1
Views: 622
Reputation: 30123
Add -noexit
argument as follows (-noprofile
is not mandatory):
import subprocess
cmd = ['powershell', '-noprofile', '-noexit', '&', 'ls *.csv']
prompt = subprocess.call ( cmd )
Result:
Upvotes: 2
Reputation: 1943
Its because you forgot to communicate
with process
Simply add a line
output, error = prompt.communicate() # this is to start the process
print(output) # add stdout=subprocess.PIPE
print(error) # add stderr=subprocess.PIPE
PS: I can't help you with powershell because i don't know powershell
Upvotes: 0
Reputation: 2529
Any reason to not use subprocess.call instead? I think it would do exactly what you want.
Upvotes: 0