PEP ? os.listdir enhancement

Ivan Van Laningham ivanlan at pauahtun.org
Thu Jun 23 11:21:55 EDT 2005


Hi All--

Thomas Guettler wrote:
> 
> I like it. But as you noticed, too, "join" would be better than "abs".
> 
> Example:
> 
> # mylistdir.py
> import os
> import sys
> 
> def mylistdir(dir, join=False):
>     for file in os.listdir(dir):
>         yield os.path.join(dir, file)
> 
> print list(mylistdir(sys.argv[1]))
> 

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))

(Note that in your version the join argument isn't used at all.)

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps:  Cu Chi, Class of '70
Author:  Teach Yourself Python in 24 Hours



More information about the Python-list mailing list