PEP ? os.listdir enhancement

Riccardo Galli riccardo_cut1 at cut2_sideralis.net
Thu Jun 23 18:48:52 CEST 2005


On Thu, 23 Jun 2005 09:21:55 -0600, Ivan Van Laningham wrote:

> Mmmm, how about:
> 
> # mylistdir.py
> import os, os.path
> import sys
> 
> def mylistdir(dir, join=False):
>     for file in os.listdir(dir):
> 	if join:
>             yield join(dir, file)
> 	else:
> 	    yield file
> 
> print list(mylistdir(sys.argv[1]))
> 
> or
> 
> print list(mylistdir(sys.argv[1],os.path.join))
> 
> That way I could def my own join and call it as
> 
> print list(mylistdir(sys.argv[1],myjoin))

I think that the implementation of listdir is done in C, so the
functionality would be added in C too.
by the way, I think 'join' (cute keyword) should be a boolean and not a
callable: the obvious way is that we join path using os.path.join, and not
some sophisticated function. With a boolean keyword code is smaller and if
we want to use our special join function we can do it simply.

e.g
def func(dir,join=False):
  return (join and join(dir,x) or x for x in os.listdir(dir))

os.listdir is actually supposed not to be a generator, like you suggested.
Are there known future changes ?

Bye,
Riccardo

-- 
Riccardo Galli
Sideralis Programs
http://www.sideralis.net



More information about the Python-list mailing list