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?

7
  • Should be fine. Did you check to see if the abcd directory was created? Commented May 4, 2014 at 8: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). Commented May 4, 2014 at 8:24
  • @sshashank124 I have checked for that directory, it didn't create one Commented May 4, 2014 at 8:54
  • @SergeBallesta I am giving the complete command in that curly braces stackoverflow.com/questions/11615455/… Commented May 4, 2014 at 8: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 ? Commented May 4, 2014 at 9:32

1 Answer 1

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"])
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Sebastian. I don't need to open a new console window. It worked.
@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 (start is a shell command)
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.