Search code examples
pythonfilefindfilesystems

Simplest way to get the equivalent of "find ." in python?


What is the simplest way to get the full recursive list of files inside a folder with python? I know about os.walk(), but it seems overkill for just getting the unfiltered list of all files. Is it really the only option?


Solution

  • There's nothing preventing you from creating your own function:

    import os
    
    def listfiles(folder):
        for root, folders, files in os.walk(folder):
            for filename in folders + files:
                yield os.path.join(root, filename)
    

    You can use it like so:

    for filename in listfiles('/etc/'):
        print filename