1

Is there a way I could send commands to Windows command prompt Python?

What I am looking to do is make a Python app on the desktop which receives commands through sockets from another computer/mobile app.

These commands are then passed on to Windows command prompt for execution.

Using Windows 7.

4
  • 2
    Do you mean the real MS-DOS, or the Windows Command Prompt? (The latter is not DOS. It's a Windows program.) Dec 23, 2010 at 12:03
  • Windows command prompt..
    – Akash
    Dec 23, 2010 at 12:10
  • are you just interested in execution, do not care how it works behind the scenes or do you really need the console (cmd.exe)?
    – akira
    Dec 23, 2010 at 18:51
  • @akira: terminology - cmd.exe is a shell, it parses commands. A console displays them and their output on screen. Dec 23, 2010 at 20:46

1 Answer 1

5

use subprocess module

from subprocess import call

call(['cmd', 'arg1', 'arg2'], stdin='...', stdout='...')

eg:

call(['dir', '/W'])

Read more here Subprocess management

3
  • Sometimes, os.system can be a better choice - for example, if you have a complete command line and do not want to modify it (which Akash's question seems to be like.) Dec 23, 2010 at 13:27
  • @grawity: subprocess.Popen() and there you have os.system()
    – akira
    Dec 23, 2010 at 18:49
  • @akira: My point was about os.system("dir /w") versus call(["dir", "/w"]) - splitting the input just to have it rejoined later, and execution via shell. Although I was wrong - I forgot about Popen(..., shell=True), which does accept a string command. Dec 23, 2010 at 20:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .