[Chicago] Reading From a Directory

Brian Curtin brian.curtin at gmail.com
Tue May 10 16:17:06 CEST 2011

On Mon, May 9, 2011 at 23:35, Clyde Forrester <clydeforrester at gmail.com>wrote:
> I don't think that you understand my objection. C has libraries for opening
> and reading directories. Python does not.

If you wanted to, you could use ctypes and access whatever POSIX shared
libraries you want and you could write the exact same code in Python as you
could in C.

> I have no objection to modules, packages and libraries. What I'm objecting
> to is the notion that Python can't inherently read directories. What goes on
> in the module? Is it secret code? Is it in another language?

The os module is 10,176 lines of C (in Modules/posixmodule.c) and 832 lines
of Python (in Lib/os.py), joined together to provide a fairly thin layer
over the platform libraries (POSIX, Windows API, etc). os.listdir in
particular boils down to around 75 lines of C, most of which is error
checking, argument conversion boilerplate (converting from a Python string
to a char* directory name), etc. Stripping all of that out, you get an
opendir call, then we loop readdir calls around that result.

> Is Python's standard solution to anything tricky to write a module in
> another language and then invoke the module from Python? Most languages just
> treat directories as a special sort of file, and just don't get all that
> worked up.

Like most high level languages, stuff like listing a directory is simplified
and abstracted away. You end up with a one line call "os.listdir(dir)"
rather than everyone re-inventing the wheel with their own opendir/readdir
loop. Directories aren't really treated as anything other than a string name
- you pass a directory name to various functions and they operate on the
directory for you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20110510/c1e59878/attachment.html>

More information about the Chicago mailing list