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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:46:09+00:00 2026-06-07T20:46:09+00:00

I have a directory structure that resembles the following: Dir1 Dir2 Dir3 Dir4 L

  • 0

I have a directory structure that resembles the following:

Dir1
Dir2
Dir3
Dir4
    L SubDir4.1
    L SubDir4.2
    L SubDir4.3

I want to generate a list of files (with full paths) that include all the contents of Dirs1-3, but only SubDir4.2 inside Dir4. The code I have so far is

import fnmatch
import os

for root, dirs, files in os.walk( '.' )
    if 'Dir4' in dirs:
        if not 'SubDir4.2' in 'Dir4':
            dirs.remove( 'Dir4' )
    for file in files
        print os.path.join( root, file )

My problem is that the part where I attempt to exclude any file that does not have SubDir4.2 in it’s path is excluding everything in Dir4, including the things I would like to remain. How should I amend that above to to do what I desire?

Update 1: I should add that there are a lot of directories below Dir4 so manually listing them in an excludes list isn’t a practical option. I’d like to be able to specify SubDur4.2 as the only subdirectory within Dir4 to be read.

Update 2: For reason outside of my control, I only have access to Python version 2.4.3.

  • 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-07T20:46:10+00:00Added an answer on June 7, 2026 at 8:46 pm

    There are a few typos in your snippet. I propose this:

    import os
    
    def any_p(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    include_dirs = ['Dir4/SubDir4.2', 'Dir1/SubDir4.2', 'Dir3', 'Dir2'] # List all your included folder names in that
    
    
    for root, dirs, files in os.walk( '.' ):
        dirs[:] = [d for d in dirs if any_p(d in os.path.join(root, q_inc) for q_inc in include_dirs)]
    
        for file in files:
            print file
    

    EDIT: According to comments, I have changed that so this is include list, instead of an exclude one.

    EDIT2: Added a any_p (any() equivalent function for python version < 2.5)

    EDIT3bis: if you have other subfolders with the same name ‘SubDir4.2’ in other folders, you can use the following to specify the location:

    include_dirs = ['Dir4/SubDir4.2', 'Dir1/SubDir4.2']
    

    Assuming you have a Dir1/SubDir4.2.

    If they are a lot of those, then you may want to refine this approach with fnmatch, or probably a regex query.

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

Sidebar

Related Questions

I have a directory structure that looks like this: basedir/dir1/dir2/dir3/dir4/myzip/subdirs I would like to
I have a directory structure of directories and files that I want to render
I have a directory structure that looks like: Foo::Bar::Baz::1 Foo::Bar::Baz::2 etc Can I list
I have a directory structure like this... dir/ build.py dir2 dir3/ packages.py Now the
I have a directory structure that looks like this: /a/f.xml /b/f.xml /c/f.xml /d/f.xml What
I have a highly structured hierarchical directory containing multiple files that need to be
I have a directory structure as follows Client_Site/ some_folder1/ some_folder2/ some_folder3/ Lots of files
I have the following directory structure: Main.py A/ __init__.py B/ __init__.py C/ __init__.py The
I have the following directory structure: schema1 schema1.xsd schema1.xsdconfig schema2 schema2.xsd schema2.xsdconfig schema3 schema3.xsd
I have a directory structure with the following on localhost: http://localhost/testing/ A directory structure

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.