Cannot seem to crawl a deep directory with my Python script, any idea?
Question: The script is basically creating a list with all the files in all directories.
Any idea why is seems to crash when it has to scan a directory that is larger
than a few files?

    
    
    import os
    
    correctlyNamedDirectories = []
    
    def crawlDirectories(directory):
        for dirname, dirnames, filenames in os.walk(directory):
            for subdirname in dirnames:
                correctlyNamedDirectories.append(os.path.join(dirname, subdirname))
            for filename in filenames:
                correctlyNamedDirectories.append(os.path.join(dirname, filename))
    
    crawlDirectories('.')
    
    print correctlyNamedDirectories
    

Also, is there a cleaner way of writing this?


Answer: Shorter method with a list comprehension:

    
    
    correctlyNamedDirectories = [os.path.join(path, subname) for path, dirnames, filenames in os.walk(directory) for subname in dirnames + filenames]
    

