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

  • SEARCH
  • Home
  • 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 6882255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:13:51+00:00 2026-05-27T05:13:51+00:00

Since I need to do many traversals of directories, which some complex filtering, I

  • 0

Since I need to do many traversals of directories, which some complex filtering, I thought to create a wrapper around os.walk.

Which is something like this:

def fwalk(root, pred_dir, pred_files, walk_function=walk):
    """Wrapper function around the standard os.walk, that filter out
    the directories visited using a filtering predicate
    """

    for base, dirs, files in walk_function(root):
        # ignore also the root directory when not needed, which is
        # actually more important than the subdirectories
        dirs = [d for d in dirs if pred_dir(path.join(base, d))]
        files = [f for f in files if pred_files(path.join(base, f))]

        if _ignore_dirs_predicate(base) and (dirs or files):
            yield base, dirs, files

Basically it behaves as os.walk, but takes two predicates to make it a bit nicer to compose in higher-level functions.
For example this will only go through the python modules:

ISA_PY = lambda f: f[-3:] == '.py'
# I can make it a class or maybe even a module if it's better
def walk_py(src):
    # should not be in the list
    return fwalk(src, _ignore_dirs_predicate, ISA_PY)

It also takes a walk function which for example can be just a dummy walk, used for testing.

def dummy_walk(_):
    test_dir = [
        ('/root/', ['d1, .git'], []),
        ('/root/d1', [], ['setup.py']),
        ('/root/test', [], ['test1.py']),
        ('/root/.git', [], [])
    ]

    # returns a function which  skips the parameter and return the iterator
    return iter(test_dir)

The problem now is that I find it very hard to trust this function, apart from the some unit testing using the dummy walk is quite hard to make sure it’s correct.

Any suggestion about how I can improve this and make it nicer?

  • 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-05-27T05:13:52+00:00Added an answer on May 27, 2026 at 5:13 am

    you need to modify dirs in place in order to avoid recursive traversal of the removed directories. Use:

    dirs[:] = [d for d in dirs if pred_dir(path.join(base, d))]
    

    this will remove the need to check _ignore_dirs_predicate(base) (and remove the NameError caused by the use of _ignore_dirs_predicate instead of pred_dir)

    You should also rewrite ISA_PY to use str.endswith()

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

Sidebar

Related Questions

I am using Foundation CSS to create some contained tabs. http://foundation.zurb.com/docs/ui.php#simpleContained1 Since I need
I am using FckEditor in Create.aspx page in asp.net mvc application. Since I need
I need something simple like date , but in seconds since 1970 instead of
I have to add many registries to a table and I need help since
I use a simulation written in python/numpy/cython. Since i need to average over many
I need to select elements without child node (including text since in <p> text
I need help converting eregi_replace to preg_replace (since in PHP5 it's depreciated): function makeClickableLinks($text)
i need to know what is the limitations and the problems for QuickFixJ since
Since i'm a poor sql developer, i need support to write a sql query
I need show a notication modal window.. But since its a fluid layout the

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.