0

I am trying make a series of commands, each separated by a carriage return, but something isn't working.

I thought that the following would be what I wanted:

subprocess.call(["PowerShell", r"cd C:\Users\Spencer", "dir"])

but it appears this simply puts each command on one line, separated by spaces

I also tried the following:

subprocess.call("PowerShell")
subprocess.call(r"cd C:\Users\Spencer")
subprocess.call("dir")

but it appears that each subprocess. call happens in a new terminal?

Thanks for any help.

spizwhiz
  • 127
  • 2
  • 7
  • 1
    You need to use the `-Command` option to the `Powershell` executable, see https://learn.microsoft.com/en-us/powershell/scripting/core-powershell/console/powershell.exe-command-line-help?view=powershell-6 – cdarke Jun 14 '18 at 18:14
  • 1
    You should look into Popen (the underlying functionality of subprocess) and the `communicate()` method that will allow you to issue commands to your subprocess. – g.d.d.c Jun 14 '18 at 18:14
  • I have spent alot of time looking at the subprocess docs but am still quite confused. A quick example of how to do the above would be much appreciated. The idea would be that each command would be followed by the enter key, just as if one were interacting with the command prompt. – spizwhiz Jun 14 '18 at 19:56
  • It looks like the answers to why the above code wasn't working as intended are here: https://stackoverflow.com/questions/21406887/subprocess-changing-directory and here: https://stackoverflow.com/questions/3022013/windows-cant-find-the-file-on-subprocess-call entering subsequent commands as above works just fine, the problem was my usage of "cd ....." (us os.chdir instead) and "dir" (need to include shell=True) – spizwhiz Jun 15 '18 at 17:19

0 Answers0