[Tutor] problem detecting files

Christopher Arndt chris.arndt at web.de
Fri Dec 2 00:58:11 CET 2005


Paul Hendrick schrieb:
> Hi there,
> I've got a problem with getting the difference between 2 directories,
> but only recognising directories and not files.
> I'm trying to find directories in /home/svn that aren't
> in /usr/local/trac/projects.
> 
> The list returned by listdir in /home/svn is:
> ['.bash_logout', '.bash_profile', '.bashrc', '.emacs', 'dir1', 'dir2']
> 
> the problem is I can't get the script to detect that .bash_profile
> and .emacs aren't directories, it always comes back in my list of
> directories.

os.listdir() always returns a list of all files in a directory. If you want
just the directories, you have to filter them out yourself:

>>> from os.path import join, isdir
>>> files = os.listdir('mydir')
>>> dirs = [f for f in files if isdir(join('mydir', f))]

Have you looked at the popular 'path' module
(http://www.jorendorff.com/articles/python/path/)? It eases common tasks when
handling filesystems very nicely.

Chris


More information about the Tutor mailing list