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 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
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
>> 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
"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
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.
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
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
Thank you all for the great info and education.
rick
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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:
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: 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
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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,...
| |