473,394 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Get all subdirs

Hello,
how can I get all subdirectories of a given directory. os.listdir(dir)
doesn't differentiate between directories and files, os.walk seems to me a
bit overkill since it also descends in the subdirs.
Thx,
Florian
Jul 18 '05 #1
5 3891
Florian Lindner wrote:
how can I get all subdirectories of a given directory

import os
def subdirs(dir): .... return [filename for filename in os.listdir(dir)
.... if os.path.isdir(os.path.join(dir, filename))]
.... subdirs('.')

['foo', 'bar', 'baz']
Jul 18 '05 #2
Florian Lindner schreef:
Hello,
how can I get all subdirectories of a given directory. os.listdir(dir)
doesn't differentiate between directories and files, os.walk seems to me a
bit overkill since it also descends in the subdirs.


#!/usr/bin/env python

import os

dir = '.' # '.' is current directory

for i in os.listdir(dir):
if os.path.isdir(os.path.join(dir, i)):
print i
Bby the way, on Linux, I noticed that the directories '.' and
'..' are not returned by os.listdir
--
Peter Kleiweg L:NL,af,da,de,en,ia,nds,no,sv,(fr,it) S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/~kleiweg/ls.html

Jul 18 '05 #3
Florian Lindner wrote:
Hello,
how can I get all subdirectories of a given directory. os.listdir(dir)
doesn't differentiate between directories and files, os.walk seems to me a
bit overkill since it also descends in the subdirs.
Thx,
Florian


What about testing return values of os.listdir with os.path.isdir?
e.g.,

filter(os.path.isdir, os.listdir(dir))

or if you don't like high-order functions,

[filename for filename in os.listdir(dir) if os.path.isdir(filename)]

--
George
Jul 18 '05 #4
Florian Lindner wrote:
Hello,
how can I get all subdirectories of a given directory. os.listdir(dir)
doesn't differentiate between directories and files, os.walk seems to me a
bit overkill since it also descends in the subdirs.
Thx,
Florian


I'm a fan of the path module:

from path import path

p = path("my_directory")
l = [i.name for i in p.dirs()]

Stephen
Jul 18 '05 #5
[Florian Lindner]
how can I get all subdirectories of a given directory. os.listdir(dir)
doesn't differentiate between directories and files, os.walk seems to me a
bit overkill since it also descends in the subdirs.


os.walk() is a generator -- it doesn't descend into anything unless
you resume it. That's the usual case, but you don't need to resume
it.

def subdirs(dir):
"Return list of the subdirectories of dir."
for root, dirs, files in os.walk(dir):
return dirs

works fine, or, perhaps more obscurely,

def subdirs(dir):
"Return list of the subdirectories of dir."
return os.walk(dir).next()[1]
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Neil | last post by:
I have an existing code base that I want to import into VS 7.1 C++. It has 200+ include files in 20+ subdirs. e.g. incl/package/classfile.h incl/package2/classfile2.h I want to add all of...
2
by: Lucas Fletcher | last post by:
Hello, I was wondering how I can reference assemblies from web.config files residing in subdirectories without having to create a new project for each web.config-containing subdirectory? I...
5
by: Ecke | last post by:
Hi, i have writte 3 projects, 1 application (.exe) and 2 DLLs. In the application i use the references to add the 2 DLLs to work with their namespaces and functionallity. But the application...
6
by: Antoine De Groote | last post by:
Google tells quite some things about it, but none of them are satisfactory. I'm on Windows, and shutil operations (e.g. move, copy) throw Permission denied all the time, for the source files. It...
2
by: Evan Carmi | last post by:
hey, i am trying to move files with a specific file-ending (.msf) to dir above their current location. my code so far works as long as all the files are on the same dir level. but how can i...
1
by: mjstenberg | last post by:
Im using a batch process function but found PHP to behave very strangely: It only reads subdirs that have caharacters in their names. Im having subdirs with integer names (0, 1, 2, 3.. ) but this...
2
by: Piotrekk | last post by:
Hi My goal is to pack directory structure ( via code ) into file. Then i need to have access to these directories packed in this file ( maybe via some collection or something ). Is there a...
2
by: Martin Marcher | last post by:
Hello, I'm playing around with os.walk and I made up del_tree(path) which I think is correct (in terms of the algorithm, but not as python wants it :)). As soon as some directory is deleted...
8
by: über | last post by:
Hello opendir(DIR, "."); @FILENAME = readdir(DIR); closedir(DIR); Now here u see a script that gets file names in dir the script is in, but how i could also readdir in every subdir the script...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.