0

I know related questions where already posted, but I cannot get a hang of them.

I have a Powershell-command and put it in Python in a string, say string aa.

How can I run command aa from Python. I know I should use subprocess, but I am not sure in what way.

Think of aa as cd ../data to get from the current folder to another folder, but also more complicated commands are possible, such as:

docker run --rm -v $pwd\xml\:C:\xml -i LocationOfRegistry powershell /C 'cat C:\xml\*.xml | python .\core-wrap\run.py' > output.csv

Thanks in advance

nippon
  • 123
  • 6

1 Answers1

0

You are looking for popen.

from subprocess import Popen, PIPE

process = Popen(['cd', path], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
Isdj
  • 1,835
  • 1
  • 18
  • 36
  • Are you sure this is to open a folder? I get an error stating that the file specified cannot be found, while the path is correct. And what if I want to run a command other that `cd`? – nippon May 30 '17 at 11:12
  • You put a different commandinstead. if you want to open a file you use `f = open(path,'r')` – Isdj May 30 '17 at 11:14
  • Edit: Adding `Shell=True` solves the first problem. How can I now run other command, like calling docker from Powershell? – nippon May 30 '17 at 11:18