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

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 2434
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
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
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
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
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
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
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
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
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
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
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.