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.)
    – user1686
    Dec 23 '10 at 12:03
  • Windows command prompt..
    – Akash
    Dec 23 '10 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 '10 at 18:51
  • @akira: terminology - cmd.exe is a shell, it parses commands. A console displays them and their output on screen.
    – user1686
    Dec 23 '10 at 20:46
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.)
    – user1686
    Dec 23 '10 at 13:27
  • @grawity: subprocess.Popen() and there you have os.system()
    – akira
    Dec 23 '10 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.
    – user1686
    Dec 23 '10 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.