Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8580833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:54:38+00:00 2026-06-11T20:54:38+00:00

this is my first post, so be gentle. ;) PROBLEM: I would like to

  • 0

this is my first post, so be gentle. 😉

PROBLEM: I would like to be able to use os.walk as a directory walker, but not do into certain folders. Ex:

Tree:

\Proj1_0
    \Load001
        \lib
        \src
\Proj2_0
    \Load001
        \lib
        \src
    \Load002
        \lib
        \src

I want to show the projects and loads, but not the sub-directories under loads. I can do that using the following code.

import os


for root, subFolders, files in os.walk('.'):
    # root does NOT contain 'Load'
    if root.find('Load') == -1:
        print "\nPROJECT: " + root + "\n"
        for folder in subFolders:
            print "    " + folder

However, the list is a big list, so I tried using del to but could not get it to work right and the same thing using lists, such as (which I got from another post here):

def my_walk(top_dir, ignore):
    for dirpath, dirnames, filenames in os.walk(top_dir):
        dirnames[:] = [
            dn for dn in dirnames
            if os.path.join(dirpath, dn) not in ignore]
        yield dirpath, dirnames, filename
list my_walk('.','Load')

But I could not get the return to work properly, either. I am new to Python and appreciate any help. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:54:40+00:00Added an answer on June 11, 2026 at 8:54 pm

    You can try the following:

    for x in os.walk('.', topdown=True):
        dirpath, dirnames, dirfiles = x
        print(dirpath, dirnames)
        dirnames[:] = filter(lambda x : not x.startswith('Load'), dirnames)
    

    From help(os.walk), you can modify the names if topdown is True, in order to restrict the search.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first post here. I'm using an existing Flash widget but would
This is my first post on SO, so be gentle. I'm not even sure
This is my first post, thx, you've been very helpful. But I'm stuck. I
This is my first post here. I have a problem. I need to take
this is my first post on this website, but I'm all the time getting
This is my first post, so please be gentle. I've been playing around with
This is my first post on this forum (so please be gentle ) I'm
this is my first post here. I need create a feed like facebook with
this is my first post on stackoverflow. Hopefully I am not disturbing anybody... :)
This is my first post here - so be gentle :-) I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.