0

I have to execute python script in windows command prompt

I am using the following command to run the command, so that the script opens the command prompt execute it

os.system("start /wait cmd /c {c:\\python27\\python.exe C:\\examples\\xml2html.py --dir c:\\Temp\\abcd c:\\tmp\\results.xml}")

I will be expecting a new directory called "abcd" created at that location and some output files created inside that.

When I run this command normally in the windows command prompt it works. I am not able to execute this in the script. Windows command prompt opens and terminates quickly.

Could any one let me know where exactly is it going wrong with the command please?

srp
  • 521
  • 11
  • 22
  • Should be fine. Did you check to see if the `abcd` directory was created? – sshashank124 May 04 '14 at 08:15
  • What are curly braces for ? Just tried in windows prompt `cmd.exe /c echo foo` correctly outputs `foo`. But `cmd.exe /c { echo foo }` gives an error (in a Vista box). – Serge Ballesta May 04 '14 at 08:24
  • @sshashank124 I have checked for that directory, it didn't create one – srp May 04 '14 at 08:54
  • @SergeBallesta I am giving the complete command in that curly braces http://stackoverflow.com/questions/11615455/python-start-new-command-prompt-on-windows-and-wait-for-it-finish-exit – srp May 04 '14 at 08:56
  • 1
    @srp ; I'm afraid the curly braces in ref'ed post were just a typografic décoration. You should try to remove them. By the way do you really need so many interpretors ? Could'ny you just import second script and directly call a python function ? – Serge Ballesta May 04 '14 at 09:32
  • unless `xml2html.py` doesn't allow it or you have a particular reason; import xml2html and use its functions directly instead of running it as a script. – jfs May 04 '14 at 10:00
  • @SergeBallesta I tried removing the curly braces and it worked. Thank you – srp May 04 '14 at 10:53

1 Answers1

2

Unless you want to open a new console window you don't need to run cmd.exe (%COMSPEC%) in order to run another Python script as a subprocess:

import sys
from subprocess import check_call

check_call([sys.executable, "C:\\examples\\xml2html.py",
            "--dir", "c:\\Temp\\abcd", "c:\\tmp\\results.xml"])
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Thank you Sebastian. I don't need to open a new console window. It worked. – srp May 04 '14 at 10:54
  • @eryksun: you *can* use `cmd` to open a new console window e.g., see [this answer that opens two new console windows, namely `new_window_command`](http://stackoverflow.com/a/19797600/4279) (`start` is a *shell* command) – jfs May 04 '14 at 14:10
  • @eryksun: you are right: it was ambiguous. I've updated the answer. The meaning is: you don't need a shell to run a Python script as a subprocess. – jfs May 04 '14 at 14:27