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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:36:22+00:00 2026-06-03T13:36:22+00:00

I’d like to recursively walk a directory, but I want python to break from

  • 0

I’d like to recursively walk a directory, but I want python to break from any single listdir if it encounters a directory with greater than 100 files. Basically, I’m searching for a (.TXT) file, but I want to avoid directories with large DPX image sequences (usually 10,000 files). Since DPXs live in directories by themselves with no sub directories, I’d like to break that loop ASAP.

So long story short, if python encounters a file matching “.DPX$” it stops listing the sub-directory, backs out, skips that sub-directory and continues the walk in other sub-directories.

Is this possible to break a directory listing loop before all the list results are returned?

  • 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-03T13:36:23+00:00Added an answer on June 3, 2026 at 1:36 pm

    The right way to avoid allocating the list of names using the os.listdir is to use the os level function as @Charles Duffy said.

    Inspired from this other post: List files in a folder as a stream to begin process immediately

    I added how to solve the specific OP question and used the re-entrant version of the function.

    from ctypes import CDLL, c_char_p, c_int, c_long, c_ushort, c_byte, c_char, Structure, POINTER, byref, cast, sizeof, get_errno
    from ctypes.util import find_library
    
    class c_dir(Structure):
        """Opaque type for directory entries, corresponds to struct DIR"""
        pass
    
    class c_dirent(Structure):
        """Directory entry"""
        # FIXME not sure these are the exactly correct types!
        _fields_ = (
            ('d_ino', c_long), # inode number
            ('d_off', c_long), # offset to the next dirent
            ('d_reclen', c_ushort), # length of this record
            ('d_type', c_byte), # type of file; not supported by all file system types
            ('d_name', c_char * 4096) # filename
            )
    c_dirent_p = POINTER(c_dirent)
    c_dirent_pp = POINTER(c_dirent_p)
    c_dir_p = POINTER(c_dir)
    
    c_lib = CDLL(find_library("c"))
    opendir = c_lib.opendir
    opendir.argtypes = [c_char_p]
    opendir.restype = c_dir_p
    
    readdir_r = c_lib.readdir_r
    readdir_r.argtypes = [c_dir_p, c_dirent_p, c_dirent_pp]
    readdir_r.restype = c_int
    
    closedir = c_lib.closedir
    closedir.argtypes = [c_dir_p]
    closedir.restype = c_int
    
    import errno
    
    def listdirx(path):
        """
        A generator to return the names of files in the directory passed in
        """
        dir_p = opendir(path)
    
        if not dir_p:
            raise IOError()
    
        entry_p = cast(c_lib.malloc(sizeof(c_dirent)), c_dirent_p)
    
        try:
            while True:
                res = readdir_r(dir_p, entry_p, byref(entry_p))
                if res:
                    raise IOError()
                if not entry_p:
                    break
                name = entry_p.contents.d_name
                if name not in (".", ".."):
                    yield name
        finally:
            if dir_p:
                closedir(dir_p)
            if entry_p:
                c_lib.free(entry_p)
    
    if __name__ == '__main__':
        import sys
        path = sys.argv[1]
        max_per_dir = int(sys.argv[2])
        for idx, entry in enumerate(listdirx(path)):
            if idx >= max_per_dir:
                break
            print entry
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:

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.