Use Python to Optimally find Top 10 recently created files
Question: I am trying to find a optimal way of extracting most recently, say 10, created
files from a directory tree using Python. I've found a number[1, 2] of
interesting solutions, however, they only involved a single file.

    
    
                          ├── d1
                          │   ├── d1-1
                          │   ├── d1-1
                          :
                          ├── d2
                          │   ├── d2-1
                          │   └── d2-2
                          │   │   ├── f1.xxx
                          : :
                          │   │   ├── fn.xxx
                          ├── d3
                          │   ├── d3-1
                          :
                          :
    

The only way I can think of doing that, at the moment, is iteratively
appending results by looping through the same tree until I have desired 10
results; the problem with this approach is that it will clearly be time
consuming... especially that my directory trees may be huge. An alternative
solution I thought about involves parsing an entire directory tree and
generating file name paths with their corresponding creation or modification
dates and then possibly use that 'index' file to get top 10 recent files. A
database would have perhaps been useful here, however, it's not an option at
the moment.

Does anyone know of an optimal way of achieving this?

[1] [Python return filepath/filename of most recent csv file stored in
directory](http://stackoverflow.com/q/11900038/664424)  
[2] [Find the most recent file in a directory without reading all the contents
of it](http://stackoverflow.com/q/4709968/664424)


Answer: You could write a generator function to return the creation time and the
filename, and use the `heapq` module to automatically keep track of the latest
'n' entries rather efficiently - example:

    
    
    import os
    import heapq
    
    def iterfiles(root):
        for base, dirs, files in os.walk(root):
            for filename in files:
                fullname = os.path.join(base, filename)
                yield os.stat(fullname).st_ctime, fullname
    
    print heapq.nlargest(10, iterfiles('some path here'))
    

Just an aside - you may have to handle an `IOError` (a try/except around the
yield) if permissions aren't available to stat the file.

