0
from subprocess import * test = subprocess.Popen('ls') print test 

When i try to run this simple code, I get an error window saying:

WindowsError: [Error 2] The system cannot find the file specified 

I have no clue why I can't get this simple code to work and it's frustrating, any help would be greatly appreciated!

4
  • 7
    do you have "ls.exe" in your path? wait, what OS you're using? Commented May 14, 2013 at 13:23
  • Yeah, so how do I make the Windows system know about the ls command? Commented May 14, 2013 at 13:45
  • 2
    @user2371187 If you want a list of files, it's simpler to use os.listdir(). Commented May 14, 2013 at 14:01
  • 1
    windows doesn't have ls (unless powershell/mingw32); it uses dir Commented May 14, 2013 at 14:30

2 Answers 2

3

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:

Sign up to request clarification or add additional context in comments.

16 Comments

I still get the same windows error.. :(
@user2371187 I run Python on Linux, but could you try my last edit? Popen('cmd', '/C', 'ls')?
@timss There's no ls binary on Windows by default, nor a command of that name in cmd.exe. There is one in powershell.exe, although its output differs significantly from that of Unix's ls. os.listdir() is probably the simplest way to replicate ls on Windows.
@Aya Oh, right of course. Been a while since I used cmd.exe. I'll update my answer.
@timss FWIW, subprocess.Popen(['powershell', '/C', 'ls']) will work, but it'd a pain in the butt to parse the output.
|
0

A agree with timss; Windows has no ls command. If you want a directory listing like ls on Windows use dir /B for single-column or dir /w /B for multi-column. Or just use os.listdir. If you do use dir, you must start subprocess using subprocess.Popen(['dir', '/b'], shell=True). If you want to store the output, use subprocess.Popen(['dir', '/b'], shell=True, stdout=subprocess.PIPE). And, the reason I used shell=True is that, since dir is an internal DOS command, the shell must be used to call it. The /b strips the header, and the /w forces multi-column output.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.