It looks like you want to store the output from a subprocess.Popen() call.
For more information see Subprocess - Popen.communicate(input=None).
>>> import subprocess >>> test = subprocess.Popen('ls', stdout=subprocess.PIPE) >>> out, err = test.communicate() >>> print out fizzbuzz.py foo.py [..]
However Windows shell (cmd.exe) doesn't have a ls command, but there's two other alternatives:
Use os.listdir() - This should be the preffered method since it's much easier to work with:
>>> import os >>> os.listdir("C:\Python27") ['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe ', 'pythonw.exe', 'README.txt', 'tcl', 'Tools', 'w9xpopen.exe']
Use Powershell - Installed by default on newer versions of Windows (>= Windows 7):
>>> import subprocess >>> test = subprocess.Popen(['powershell', '/C', 'ls'], stdout=subprocess.PIPE) >>> out, err = test.communicate() >>> print out Directory: C:\Python27 Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 14.05.2013 16:00 DLLs d---- 14.05.2013 16:01 Doc [..]
Shell commands using cmd.exe would be something like this:
test = subprocess.Popen(['cmd', '/C', 'ipconfig'], stdout=subprocess.PIPE)
For more information see:
The ever useful and neat subprocess module - Launch commands in a terminal emulator - Windows
Notes:
os.listdir().dir