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 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']
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
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
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
[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] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |