Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
2  
Do you mean the real MS-DOS, or the Windows Command Prompt? (The latter is not DOS. It's a Windows program.) –  grawity 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. –  grawity Dec 23 '10 at 20:46
add comment

1 Answer

up vote 5 down vote accepted

use subprocess module

from subprocess import call

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

eg:

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

Read more here Subprocess management

share|improve this answer
    
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.) –  grawity 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. –  grawity Dec 23 '10 at 20:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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