Moving specific files in subdirectories into a directory - python

First, welcome to the community, and python! You might want to change your user name, especially if you frequent here. :)

I suggest the following (stolen from Mr. Beazley):

# genfind.py
#
# A function that generates files that match a given filename pattern

import os
import shutil
import fnmatch

def gen_find(filepat,top):
    for path, dirlist, filelist in os.walk(top):
        for name in fnmatch.filter(filelist,filepat):
            yield os.path.join(path,name)

# Example use

if __name__ == '__main__':
    src = 'O:\DATA\ASTER GDEM\Original\North America\UTM Zone 14\USA\Extracted' # input
    dst = 'O:\DATA\ASTER GDEM\Original\North America\UTM Zone 14\USA\Analyses' # desired     location

    filesToMove = gen_find("*dem.tif",src)
    for name in filesToMove:
        shutil.move(name, dst)

Update: the questioner has clarified below that he / she is actually calling the move function, which was the first point in my answer.

There are a few other things to consider:

  • You've got the order of elements returned in each tuple from os.walk wrong, I'm afraid - check the documentation for that function.
  • Assuming you've fixed that, also bear in mind that you need to iterate over files, and you need to os.join each of those to root, rather than src
  • The above would be obvious, hopefully, if you print out the values returned by os.walk and comment out the rest of the code in that loop.
  • With code that does potentially destructive operations like moving files, I would always first try some code that just prints out the parameters to shutil.move until you're sure that it's right.

I think you've mixed up the way you should be using os.walk().

for dirpath, dirs, files in os.walk(src):
    print dirpath
    print dirs
    print files
    for filename in files:
        if filename.endswith('dem.tif'):
            shutil.move(...)
        else:
            ...