using os

Jeremy Jones zanesdad at bellsouth.net
Mon Nov 29 18:03:54 EST 2004


Jerry Sievers wrote:

>Juliano Freitas <jubafre at atlas.ucpel.tche.br> writes:
>
>  
>
>>how can i get just the directories in other directorie without a
>>files using the os module in Python??
>>    
>>
>
>If there's a command for listing only dirs, I've not stumbled on it.
>
>Here's a one-liner using filter and lambda;
>
>from os import listdir
>from os.path import isdir
>
>dir = '/tmp/'
>
>onlyDirs = filter(lambda entry: isdir(dir + entry), listdir(dir))
>
>HTH
>
>
>  
>
Or using list comprehensions:

 >>> [f for f in os.listdir('/tmp') if 
os.path.isdir(os.path.join('/tmp', f))]
['gconfd-root', '.X11-unix', '.ICE-unix', '.mozilla', '.font-unix']

Just as a side note, and just preference, but I typically (almost 
always) prefer using os.path.join() rather than using the "+" operator 
to put a path together.


Jeremy Jones
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20041129/dcd34170/attachment.html>


More information about the Python-list mailing list