473,657 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3907
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(o s.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(o s.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,e n,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(f ilename)]

--
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_direct ory")
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).ne xt()[1]
Jul 18 '05 #6

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

Similar topics

0
825
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 these files recursively to the project. Is there a known trick/utility to do this ?
2
2926
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 would like to use an HttpModule, but only for files in a particular directory, so I add a web.config file containing this: <?xml version="1.0" encoding="utf-8" ?>
5
1233
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 always copys the DLLs in its own diretory. I want to have my own structure e.g.:
6
14559
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 seems that this is the case for all my files. But what I don't understand is that yesterday it still worked. I didn't change anything on my system though (at least not that I am aware of). I restarted the computer several times to see if that...
2
1435
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 make it work if there are multiple subdirs with files inside of them on different dir levels?
1
1146
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 script does not read them at all?! Can someone tell me how to get around this? function readSubdir($dirPath) {
2
3809
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 common way to do this? Thanks for help and Regards PK
2
3014
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 the iterator of os.walk chokes. OK that is somehow clear to me as it can't be valid anymore since it can't go to the just deleted directory but it is in the iterator.
8
3343
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 directory has?
0
8407
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8319
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8512
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8612
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6175
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.