How to execute a Command Prompt Command from Python

how about simply:

import os
os.system('dir c:\\')

Try:

import os

os.popen("Your command here")

You probably want to try something like this:

command = "cmd.exe /C dir C:\\"

I don't think you can pipe into cmd.exe... If you are coming from a unix background, well, cmd.exe has some ugly warts!

EDIT: According to Sven Marnach, you can pipe to cmd.exe. I tried following in a python shell:

>>> import subprocess
>>> proc = subprocess.Popen('cmd.exe', stdin = subprocess.PIPE, stdout = subprocess.PIPE)
>>> stdout, stderr = proc.communicate('dir c:\\')
>>> stdout
'Microsoft Windows [Version 6.1.7600]\r\nCopyright (c) 2009 Microsoft Corporatio
n.  All rights reserved.\r\n\r\nC:\\Python25>More? '

As you can see, you still have a bit of work to do (only the first line is returned), but you might be able to get this to work...

Tags:

Python

Windows