Python : Searching a directory for files in a list
Question: ## Context

I have a list of filenames (many tens of thousands) that I would like to
locate within a directory. For all files that are located they must be copied
to a single output folder.

Using Python, what would be, **in your opinion, the most efficient* strategy**
for doing this? I'm not looking for a solution, but good strategy to get
started.

## To break this down:

  * How the list of filenames should be stored and the method of searching if a filename is in the list?
  * How to go through an entire directory, folder by folder, and consider each file in tern?
  * How to copy the file (least processing time)?

## Caveat

*efficient in the sense that the script execution should not 'hog' system's resources. Other more important applications maybe running concurrently. 

Many thanks!


Answer: 
    import os
    import shutil
    
    filenames_i_want = set() # fill this with the filenames you want
    dest_dir = 'whatever'
    src_dir = 'whatever'
    
    for (dirpath, dirnames, filenames) in os.walk(src_dir):
        for fname in filenames:
            if fname in filenames_i_want:
                shutil.copy(os.path.join(dirpath, fname), dest_dir)
    

If this proves too slow [use a
profiler](https://docs.python.org/2/library/profile.html) to figure out the
slow parts and optimize from there.

If you find that `shutil.copy` is slow, refer to ["Python copy larger file too
slow"](http://stackoverflow.com/questions/21799210/python-copy-larger-file-
too-slow).

