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 59675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:02:33+00:00 2026-05-10T18:02:33+00:00

How do I limit os.walk to only return files in the directory I provide

  • 0

How do I limit os.walk to only return files in the directory I provide it?

def _dir_list(self, dir_name, whitelist):     outputList = []     for root, dirs, files in os.walk(dir_name):         for f in files:             if os.path.splitext(f)[1] in whitelist:                 outputList.append(os.path.join(root, f))             else:                 self._email_to_('ignore')     return outputList 
  • 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. 2026-05-10T18:02:33+00:00Added an answer on May 10, 2026 at 6:02 pm

    Use the walklevel function.

    import os  def walklevel(some_dir, level=1):     some_dir = some_dir.rstrip(os.path.sep)     assert os.path.isdir(some_dir)     num_sep = some_dir.count(os.path.sep)     for root, dirs, files in os.walk(some_dir):         yield root, dirs, files         num_sep_this = root.count(os.path.sep)         if num_sep + level <= num_sep_this:             del dirs[:] 

    It works just like os.walk, but you can pass it a level parameter that indicates how deep the recursion will go.

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

Sidebar

Related Questions

I want to limit my users to a directory and its sub directories but
How can I limit my post-build events to running only for one type of
With :limit in query, I will get first N records. What is the easiest
Is there a security limit to the number of Ajax XMLHttpRequest objects you can
I'd like to limit the size of the file that can be uploaded to
How do I limit the types of HTML that a user can input into
Is there a connection limit on Sql Server 2005 Developers Edition. We have many
What is the upper limit for an autoincrement primary key in SQL Server? What
How do I limit a SQL Server Profiler trace to a specific database? I
How do you do LIMIT in DB2 for iSeries? I have a table with

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.