473,386 Members | 1,715 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,386 software developers and data experts.

Gettings subdirectories

Hello,
how can I get all subdirectories of a given directories? os.listdir() gives
me all entries and I've found no way to tell if an object is a file or a
directory.

Thanks,

Florian
May 3 '06 #1
10 1540
Florian Lindner enlightened us with:
how can I get all subdirectories of a given directories?
os.listdir() gives me all entries and I've found no way to tell if
an object is a file or a directory.


Why, doesn't your os.path.isdir() function work?

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
May 3 '06 #2
It's a bit overkill, but consider using os.walk.

root, dirnames, filenames = os.walk(r"C:\").next() # (in real code,
you'd want to catch exceptions here)
print dirnames

Florian Lindner wrote:
Hello,
how can I get all subdirectories of a given directories? os.listdir() gives
me all entries and I've found no way to tell if an object is a file or a
directory.

Thanks,

Florian


May 3 '06 #3
>> root, dirnames, filenames = os.walk(r"C:\").next()

Wow. How does that work? Just point me to where I can read about it. I
don't see it under os.walk.

That's cool.

Thanks,

Rick

May 3 '06 #4
"BartlebyScrivener" <rp*******@gmail.com> writes:
root, dirnames, filenames = os.walk(r"C:\").next()


Wow. How does that work? Just point me to where I can read about it. I
don't see it under os.walk.


We must be reading different Python websites.

walk(top[, topdown=True [, onerror=None]])

walk() generates the file names in a directory tree, by walking
the tree either top down or bottom up. For each directory in the
tree rooted at directory top (including top itself), it yields a
3-tuple (dirpath, dirnames, filenames).

<URL:http://docs.python.org/lib/os-file-dir.html#l2h-1638>

--
\ "Sittin' on the fence, that's a dangerous course / You can even |
`\ catch a bullet from the peace-keeping force" -- Dire Straits, |
_o__) _Once Upon A Time In The West_ |
Ben Finney

May 3 '06 #5
Ben Finney wrote:
We must be reading different Python websites.

walk(top[, topdown=True [, onerror=None]])

walk() generates the file names in a directory tree, by walking
the tree either top down or bottom up. For each directory in the
tree rooted at directory top (including top itself), it yields a
3-tuple (dirpath, dirnames, filenames).


Maybe he meant os.path.walk, although that's still not quite what he had.

May 3 '06 #6
Sorry that I was unclear.

I sorta know how os.walk works. It's the .next() trick that I had never
seen before. For instance, if you run that statement without the
..next() on it, it says "Too many items to unpack" but with the .next()
it stops it somehow, right where I want it to stop.

It's an iterator method, right? I found it in Beazely, now I'll try to
understand it. Sorry to trouble you.

rick

May 3 '06 #7
Florian Lindner wrote:
Hello,
how can I get all subdirectories of a given directories? os.listdir() gives
me all entries and I've found no way to tell if an object is a file or a
directory.

Thanks,

Florian

Here is a quick hack:

import os
import os.path

givenDir = "/"
listing = os.listdir(givenDir)
for item in listing:
joinPath = os.path.join(givenDir, item)
normPath = os.path.normpath(joinPath)
if os.path.isdir(normPath):
print normPath
May 4 '06 #8
Thank you all for the great info and education.

rick

May 4 '06 #9
Hi,

The second edition of "Programming Python - O'REILLY - Mark Lutz" shows how
to do that using "os.path.walk"

Philippe

Florian Lindner wrote:
Hello,
how can I get all subdirectories of a given directories? os.listdir()
gives me all entries and I've found no way to tell if an object is a file
or a directory.

Thanks,

Florian


May 4 '06 #10
Florian Lindner wrote:
how can I get all subdirectories of a given directories?


If you're not adverse to a solution outside of the standard lib, I
highly recommend the 'path' module:
from path import path
c = path("C:\\")
c.dirs()

[path(u'C:\\cmdcons'), path(u'C:\\Config.Msi'), path(u'C:\\Logon'),
path(u'C:\\Program Files'), path(u'C:\\Python24'),
path(u'C:\\RECYCLER'), path(u'C:\\System Volume Information'),
path(u'C:\\WINDOWS'), path(u'C:\\WINSRC')]

http://www.jorendorff.com/articles/python/path/

Hope this helps.

- alex23

May 5 '06 #11

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

Similar topics

1
by: Thieum22 | last post by:
Hi, I try to go through a directory and it's subdirectories to reah the properties of each files. But I have a problem to set active the directory where the files are, in order to display their...
21
by: AES/newspost | last post by:
My understanding -- I'm not an expert -- is that on (some? many? all?) standard Internet servers a URL can point to a subdirectory name followed by a backslash, and that links to this URL will...
3
by: Neil Ginsberg | last post by:
I need to write some code that will get all filenames under a particular directory and add them to a table, including any in subdirectories. I realize that Dir can be used to get all filenames in a...
2
by: Arjen | last post by:
Hello, I want to make a command line application that search for *.htm* files inside directories and subdirectories. 1. How can I save all the *.htm* files inside directories and...
1
by: Benton | last post by:
Hi there, I want to have an unrestricted root directory and some protected subdirectories on my ASP.NET 2.0 application. I want each subdirectory to have its own Login.aspx page. The...
2
by: kamalak | last post by:
hi, can someone help me in writing code in C# for getting the no of files in a directory and all its subdirectories and their subdirectories where the no of subdirectories and their...
2
by: dj | last post by:
Hello All, I am attempting to us os.walk to populate two lists with values from a directory. The first list contains all the files in the directory and subdirectories. The second list contains...
4
by: Laharl | last post by:
My Operating Systems professor has assigned homework that basically boils down to implementing ls -lra, but with a different output format. In other words, list the files and subdirectories (and a...
10
by: beary | last post by:
Hello all, I've done something a bit stupid and am hoping some kind soul out there can help me out. There's a piece of code that goes through and creates a high number of subdirectories within...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.