Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Gettings subdirectories

21 views
Skip to first unread message

Florian Lindner

unread,
May 3, 2006, 2:52:32 PM5/3/06
to
Hello,
how can I get all subdirectories of a given directories? os.listdir() gives
me all entries and I've found no way to tell if an object is a file or a
directory.

Thanks,

Florian

Sybren Stuvel

unread,
May 3, 2006, 3:02:42 PM5/3/06
to
Florian Lindner enlightened us with:

> how can I get all subdirectories of a given directories?
> os.listdir() gives me all entries and I've found no way to tell if
> an object is a file or a directory.

Why, doesn't your os.path.isdir() function work?

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa

johnz...@gmail.com

unread,
May 3, 2006, 3:15:20 PM5/3/06
to
It's a bit overkill, but consider using os.walk.

root, dirnames, filenames = os.walk(r"C:\").next() # (in real code,
you'd want to catch exceptions here)
print dirnames

BartlebyScrivener

unread,
May 3, 2006, 6:38:24 PM5/3/06
to
>> root, dirnames, filenames = os.walk(r"C:\").next()

Wow. How does that work? Just point me to where I can read about it. I
don't see it under os.walk.

That's cool.

Thanks,

Rick

Ben Finney

unread,
May 3, 2006, 7:00:59 PM5/3/06
to pytho...@python.org
"BartlebyScrivener" <rpdo...@gmail.com> writes:

> >> root, dirnames, filenames = os.walk(r"C:\").next()
>
> Wow. How does that work? Just point me to where I can read about it. I
> don't see it under os.walk.

We must be reading different Python websites.

walk(top[, topdown=True [, onerror=None]])

walk() generates the file names in a directory tree, by walking
the tree either top down or bottom up. For each directory in the
tree rooted at directory top (including top itself), it yields a
3-tuple (dirpath, dirnames, filenames).

<URL:http://docs.python.org/lib/os-file-dir.html#l2h-1638>

--
\ "Sittin' on the fence, that's a dangerous course / You can even |
`\ catch a bullet from the peace-keeping force" -- Dire Straits, |
_o__) _Once Upon A Time In The West_ |
Ben Finney

Edward Elliott

unread,
May 3, 2006, 7:28:41 PM5/3/06
to
Ben Finney wrote:
> We must be reading different Python websites.
>
> walk(top[, topdown=True [, onerror=None]])
>
> walk() generates the file names in a directory tree, by walking
> the tree either top down or bottom up. For each directory in the
> tree rooted at directory top (including top itself), it yields a
> 3-tuple (dirpath, dirnames, filenames).

Maybe he meant os.path.walk, although that's still not quite what he had.

BartlebyScrivener

unread,
May 3, 2006, 7:32:09 PM5/3/06
to
Sorry that I was unclear.

I sorta know how os.walk works. It's the .next() trick that I had never
seen before. For instance, if you run that statement without the
.next() on it, it says "Too many items to unpack" but with the .next()
it stops it somehow, right where I want it to stop.

It's an iterator method, right? I found it in Beazely, now I'll try to
understand it. Sorry to trouble you.

rick

Adonis

unread,
May 3, 2006, 8:49:49 PM5/3/06
to


Here is a quick hack:

import os
import os.path

givenDir = "/"
listing = os.listdir(givenDir)
for item in listing:
joinPath = os.path.join(givenDir, item)
normPath = os.path.normpath(joinPath)
if os.path.isdir(normPath):
print normPath

BartlebyScrivener

unread,
May 3, 2006, 11:08:07 PM5/3/06
to
Thank you all for the great info and education.

rick

Philippe Martin

unread,
May 4, 2006, 8:42:41 AM5/4/06
to
Hi,

The second edition of "Programming Python - O'REILLY - Mark Lutz" shows how
to do that using "os.path.walk"

Philippe

alex23

unread,
May 4, 2006, 9:56:57 PM5/4/06
to
Florian Lindner wrote:
> how can I get all subdirectories of a given directories?

If you're not adverse to a solution outside of the standard lib, I
highly recommend the 'path' module:

>>> from path import path
>>> c = path("C:\\")
>>> c.dirs()
[path(u'C:\\cmdcons'), path(u'C:\\Config.Msi'), path(u'C:\\Logon'),
path(u'C:\\Program Files'), path(u'C:\\Python24'),
path(u'C:\\RECYCLER'), path(u'C:\\System Volume Information'),
path(u'C:\\WINDOWS'), path(u'C:\\WINSRC')]

http://www.jorendorff.com/articles/python/path/

Hope this helps.

- alex23

0 new messages