Kobe Kobe - 5 months ago 30
Python Question

How to execute a command prompt command from python

I tried something like this, but with no effect:

command = "cmd.exe"
proc = subprocess.Popen(command, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
proc.stdin.write("dir c:\\")


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...