Sunday, August 27, 2017

Use Python to find specific files from one location to another location

import os
import shutil
from fnmatch import fnmatch


root = r'H:'
dst_dir = r'G:'
pattern = "*.tif"
i = 1
for path, subdirs, files in os.walk(root):
  for name in files:
    if fnmatch(name, pattern):
      current_file = os.path.join(path, name)
      shutil.copy(current_file, dst_dir)
      dst_file = os.path.join(dst_dir, name)
      dst_new_file_name = os.path.join(dst_dir, "["+ str(i) +"] " + name)
      os.rename(dst_file, dst_new_file_name)
      print(os.path.join(path, name))
      i += 1

No comments:

Post a Comment