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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:45:29+00:00 2026-06-11T08:45:29+00:00

I am looking for a way to iterate through a directory containing 100,000’s of

  • 0

I am looking for a way to iterate through a directory containing 100,000’s of files. Using os.listdir is TERRIBLY slow because this function first takes in the path list from the whole specified path.

What are the fastest options?

NOTE: whoever downvoted has never faced this situation for sure.

  • 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-11T08:45:31+00:00Added an answer on June 11, 2026 at 8:45 am

    This other question was referred to in comments as a duplicate:
    List files in a folder as a stream to begin process immediately

    …But I found the example to be semi not working. Here is the fixed version that works for me:

    from ctypes import CDLL, c_int, c_uint8, c_uint16, c_uint32, c_char, c_char_p, Structure, POINTER
    from ctypes.util import find_library
    
    import os
    
    class c_dir(Structure):
        pass
    
    class c_dirent(Structure):
        _fields_ = [ 
            ("d_fileno", c_uint32), 
            ("d_reclen", c_uint16),
            ("d_type", c_uint8), 
            ("d_namlen", c_uint8),
            ("d_name", c_char * 4096),
            # proper way of getting platform MAX filename size?
            # ("d_name", c_char * (os.pathconf('.', 'PC_NAME_MAX')+1) ) 
        ]
    
    c_dirent_p = POINTER(c_dirent)
    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
    
    # FIXME Should probably use readdir_r here
    readdir = c_lib.readdir
    readdir.argtypes = [c_dir_p]
    readdir.restype = c_dirent_p
    
    closedir = c_lib.closedir
    closedir.argtypes = [c_dir_p]
    closedir.restype = c_int
    
    def listdir(path):
        """
        A generator to return the names of files in the directory passed in
        """
        dir_p = opendir(".")
        try:
            while True:
                p = readdir(dir_p)
                if not p:
                    break
                name = p.contents.d_name
                if name not in (".", ".."):
                    yield name
        finally:
            closedir(dir_p)
    
    
    if __name__ == "__main__":
        for name in listdir("."):
            print name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just looking for a nicer and more efficient way to iterate through a
I'm looking for the best way to iterate through a folder and put all
I am using codeigniter and looking a way to enable directly editting of doc
I am looking for a clean way to forward some demo data using a
In Java, is it faster to iterate through an array the old-fashioned way, for
Im trying to iterate through a Treemap of the class Tile() using: Map map
I'm wondering if there's an easy way to iterate through a fd_set? The reason
Using jQuery or straight Javascript, I'm looking for the best way to find the
What is an efficient way to iterate through only the odd members of a
I'm looking for way to create list view as like in IOS with pinned

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.