Q:

Faster way to get a directory listing than invoking "ls" in a subprocess

After a search, and some test runs both os.popen()+read() and subprocess.check_output() seem to be almost equivalent for reading out the contents of a folder. Is there a way to improve either a combination of os.popen()+read() or subprocess.check_output()? I have to ls a number of folders and read the outputs, and using either of the above is similar, but represents the major bottleneck according to profiling results.

A:

You are looking for os.listdir and/or os.walk, and perhaps also the os.stat family of functions.  These are (Python bindings to) the same primitives that ls uses itself, so anything you can do by parsing the output of ls you can do with these.  I recommend you read carefully through everything the os, os.path, and stat modules offer; there may be other things you don't need an external program to do.
You should probably also read the documentation for stat, the underlying system call -- it's C-oriented, but it'll help you understand what os.stat does.

