471,330 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Directory


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
4 1042
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
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
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
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.

Similar topics

2 posts views Thread by Dave Johnston | last post: by
8 posts views Thread by James Owens | last post: by
4 posts views Thread by Elmo Watson | last post: by
8 posts views Thread by theWizard1 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.