473,503 Members | 241 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

os.path.walk -- Can You Limit Directories Returned?

Greetings all.

I did some searching on this but I can't seem to find a specific
solution. I have code like this:

=========================================
def walker1(arg, dirname, names):
DC_List.append((dirname,''))

os.path.walk('\\\\vcdcflx006\\Flex\\Sites', walker1, 0)
=========================================

The Sites\ directory is set up like this:

Sites\
Baltimore
Birmingham
....

And so forth. Each of the city directories has directories under it as
well. The problem is that my code grabs every single directory that is
under the various city directories when what I really want it to do is
just grab the directories that are under Sites\ and that's it. I don't
want it to recurse down into the sub-directories of the cities.

Is there a way to do this? Or is os.path.walk not by best choice here?

Any help and/or advice would be appreciated.

- Jeff
Jun 27 '08 #1
4 2446
Jeff Nyman schrieb:
Greetings all.

I did some searching on this but I can't seem to find a specific
solution. I have code like this:

=========================================
def walker1(arg, dirname, names):
DC_List.append((dirname,''))

os.path.walk('\\\\vcdcflx006\\Flex\\Sites', walker1, 0)
=========================================

The Sites\ directory is set up like this:

Sites\
Baltimore
Birmingham
....

And so forth. Each of the city directories has directories under it as
well. The problem is that my code grabs every single directory that is
under the various city directories when what I really want it to do is
just grab the directories that are under Sites\ and that's it. I don't
want it to recurse down into the sub-directories of the cities.

Is there a way to do this? Or is os.path.walk not by best choice here?

Any help and/or advice would be appreciated.
look into the modules glob and os.

Diez
Jun 27 '08 #2
Jeff Nyman wrote:
Greetings all.

I did some searching on this but I can't seem to find a specific
solution. I have code like this:

=========================================
def walker1(arg, dirname, names):
DC_List.append((dirname,''))

os.path.walk('\\\\vcdcflx006\\Flex\\Sites', walker1, 0)
=========================================

The Sites\ directory is set up like this:

Sites\
Baltimore
Birmingham
....

And so forth. Each of the city directories has directories under it as
well. The problem is that my code grabs every single directory that is
under the various city directories when what I really want it to do is
just grab the directories that are under Sites\ and that's it. I don't
want it to recurse down into the sub-directories of the cities.

Is there a way to do this? Or is os.path.walk not by best choice here?
Yes. But first, use the more modern iterator os.walk instead of the
older function calling os.path.walk. Then in either case (or at least
for the os.walk -- I'm a little rusty on the older os.path.walk) you can
modify in-place the subdirectory listing that was passed to you, thereby
controlling which subdirectories the walk follows.

Here's some examples:

for path, dirs, files in os.walk(root):
if 'etc' in dirs:
dirs.remove('etc') # Skip any directory named 'etc'
if path == 'whatever':
del dirs[:] # Clearing dirs means recurse into NO
subdirectory of path
... process the files of directory path...
Gary Herron
Any help and/or advice would be appreciated.

- Jeff
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #3
On Jun 5, 4:54 pm, Jeff Nyman <jeffny...@gmail.comwrote:
The problem is that my code grabs every single directory that is
under the various city directories when what I really want it to do is
just grab the directories that are under Sites\ and that's it. I don't
want it to recurse down into the sub-directories of the cities.

Is there a way to do this? Or is os.path.walk not by best choice here?
No, os.path.walk will always recurse through all of the sub, that's
its purpose. os.walk produces a generator, which you can then manually
step through if you wish:

_, DC_List, _ = os.walk('\\\\vcdcflx006\\Flex\\Sites\\*\\').next()

But I'd recommend checking out the glob module:

from glob import glob
DC_List = glob('\\\\vcdcflx006\\Flex\\Sites\\*\\')
Jun 27 '08 #4
Thank you to everyone for your help.

Much appreciated. I now have a better understanding of how glob can be
used and I have a much better understanding of using the more
effective os.walk.
- Jeff
Jun 27 '08 #5

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

Similar topics

31
3869
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
3
1657
by: Haim Ashkenazi | last post by:
Hi I have to write a function that returns a list of all directories under <PATH> (should work on linux and windows). it should be easy to write a loop that goes through every directory... and...
4
8061
by: Florian Lindner | last post by:
Hello, when I'm walking through a file system hierarchy using os.walk, how can I get the full path of a file or dir? normpath and abspath don't work. Thx, Florian
11
3205
by: rbt | last post by:
Is there an easy way to exclude binary files (I'm working on Windows XP) from the file list returned by os.walk()? Also, when reading files and you're unsure as to whether or not they are ascii...
10
2922
by: wo_shi_big_stomach | last post by:
Newbie to python writing a script to recurse a directory tree and delete the first line of a file if it contains a given string. I get the same error on a Mac running OS X 10.4.8 and FreeBSD 6.1. ...
8
2268
by: Andre Meyer | last post by:
Hi all os.walk() is a nice generator for performing actions on all files in a directory and subdirectories. However, how can one use os.walk() for walking through two hierarchies at once? I want...
10
1347
by: fscked | last post by:
I am walking some directories looking for a certain filename pattern. This part works fine, but what if I want to exclude results from a certain directory being printed? eg ...
4
1333
by: Joe Ardent | last post by:
Good day, everybody! From what I can tell from the archives, this is everyone's favorite method from the standard lib, and everyone loves answering questions about it. Right? :) Anyway, my...
0
2020
by: Jeff McNeil | last post by:
Your args are fine, that's just the way os.path.walk works. If you just need the absolute pathname of a directory when given a relative path, you can always use os.path.abspath, too. A couple...
0
7203
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
7089
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
7282
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,...
0
7339
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
7463
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...
0
5581
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,...
1
5017
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...
0
4678
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...
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.