1

I want to be able to run cmd.exe from a Python script and a particular command with arguments but I can't seem to get it working.

I have tried

cmdL = r"C:\WINDOWS\system32\cmd.exe"
schTasks = "schtasks.exe"
#run the schtasks.exe
os.system(cmdL + schTasks)

but the dos window appears and then disappears quickly.

What am I doing wrong?

Thanks

4
  • 1
    Why do you think it should stick around? Commented Aug 28, 2012 at 13:55
  • You should be using the subprocess module. See: stackoverflow.com/a/912847/1224443 Commented Aug 28, 2012 at 13:57
  • 1
    I don't know much about windows, but it seems like there should be a space between cmd.exe and schtasks.exe. Also, you should probably look into subprocess. Commented Aug 28, 2012 at 13:58
  • because when I only add the cmdL variable to os.system, it doesnt disappear. Commented Aug 28, 2012 at 13:58

2 Answers 2

3

The modern way of executing processes from a Python script is by using the subprocess module:

import subprocess
subprocess.call(["schtasks.exe"])

You can also pass arguments, for example:

subprocess.call(["ls", "-l"])

This means you won't need to utilize cmd.exe. Python will execute the given executable with the given parameters as desired.

Sign up to request clarification or add additional context in comments.

Comments

0

You forget the space between cmd.exe and the argument.

os.system("%s %s" % (cmdL, schTasks))

Nonetheless is the better way to go via subprocess like the other answer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.