By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

Directory

P: n/a

I would like to get a list of sub directories in a directory.
If I use os.listdir i get a list of directories and files in that .
i only want the list of directories in a directory and not the files
in it.
anyone has an idea regarding this.

Jul 30 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Rohan wrote:
I would like to get a list of sub directories in a directory.
If I use os.listdir i get a list of directories and files in that .
i only want the list of directories in a directory and not the files
in it.
anyone has an idea regarding this.
Look up os.walk (allows you to selectively walk over files and directories)
and/or os.path.isdir (allows you to test if a given path is a directory)

--irmen
Jul 30 '07 #2

P: n/a
On 30 Lip, 23:43, Rohan <vodela....@gmail.comwrote:
I would like to get a list of sub directories in a directory.
If I use os.listdir i get a list of directories and files in that .
i only want the list of directories in a directory and not the files
in it.
anyone has an idea regarding this.
Hi Rohan,

http://diveintopython.org/file_handling/os_module.html

Hope this helps,

Cheers,
Marek

Jul 31 '07 #3

P: n/a
Rohan wrote:
I would like to get a list of sub directories in a directory.
If I use os.listdir i get a list of directories and files in that .
i only want the list of directories in a directory and not the files
in it.
anyone has an idea regarding this.
How far down do you want to go?

All the way to the bottom:

for root, dirs, files in os.walk('.'):
for d in dirs:
print os.path.join(root, d)

####################################

Only your current directory:

x = os.listdir('.')
for item in x:
if os.path.isdir(item):
print item
Jul 31 '07 #4

P: n/a
Rohan wrote:
I would like to get a list of sub directories in a directory.
If I use os.listdir i get a list of directories and files in that .
i only want the list of directories in a directory and not the files
in it.
anyone has an idea regarding this.
import os
listofdirs=[d for d in os.listdir(mydir) if os.pathisdir(d)]

-Larry
Aug 2 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.