Skip to content

Instantly share code, notes, and snippets.

@nrrb
Created March 28, 2012 22:23
Show Gist options
  • Save nrrb/2231071 to your computer and use it in GitHub Desktop.
Save nrrb/2231071 to your computer and use it in GitHub Desktop.
import os
import shutil
import sys
# SEARCH_PATH = '.'
# DESTINATION_PATH = ''
def find_files_by_extension(search_path, extension):
all_files = []
for root, dirs, files in os.walk(search_path):
for file in files:
if file.split('.')[-1][:len(extension)].lower() == extension:
all_files.append(os.path.join(root, file))
return all_files
def copy_files(file_list, destination):
for file in file_list:
directory_branch = os.path.dirname(file)
new_dest = os.path.join(DESTINATION_PATH, directory_branch)
if not os.path.exists(new_dest):
os.makedirs(new_dest)
print 'Copying %s...' % file
shutil.copy(file, new_dest)
if __name__=="__main__":
if len(sys.argv) != 3:
print 'Syntax: python copy_xls.py search_path destination_path'
exit(0)
SEARCH_PATH = sys.argv[1]
DESTINATION_PATH = sys.argv[2]
excel_files = find_files_by_extension(SEARCH_PATH, 'xls')
copy_files(excel_files, DESTINATION_PATH)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment