Created
August 11, 2011 18:34
-
-
Save jonschoning/1140381 to your computer and use it in GitHub Desktop.
copy_matches_to_folder.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os,shutil | |
def __main(): | |
kwargs={ | |
'source_directory' :r'\\host\everyone\s\GTCom' | |
,'file_types' :['.jpg', '.gif', '.png', '.tif'] | |
,'dest_directory_root' :r'\\host\everyone\staticfiles' | |
} | |
copy_matches_to_folder(**kwargs) | |
def copy_matches_to_folder(source_directory, file_types, dest_directory_root): | |
count=0 | |
print "source_directory: ", source_directory | |
print "file_types: ", file_types | |
print "dest_directory_root: ", dest_directory_root | |
#normalize paths | |
dest_directory_root=os.path.abspath(dest_directory_root) | |
source_directory=os.path.abspath(source_directory) | |
source_filelists=[map(lambda x: os.path.join(t[0],x),t[2]) for t \ | |
in os.walk(source_directory) if len(t[2]) > 0] | |
source_files=[file for filelist in source_filelists for file in filelist] | |
#find matching files and build final paths | |
source_to_dest_filemap= \ | |
[(matched_source_file, | |
matched_source_file.replace(source_directory,os.path.abspath(dest_directory_root))) \ | |
for matched_source_file \ | |
in source_files \ | |
if os.path.splitext(matched_source_file)[1].lower() in file_types] | |
#process file map | |
if source_to_dest_filemap: | |
for source_file, dest_file in source_to_dest_filemap: | |
if not os.path.exists(os.path.dirname(dest_file)): | |
os.makedirs(os.path.dirname(dest_file)) | |
shutil.copy2(source_file, dest_file) | |
print '{0} -> {1}'.format(source_file, dest_file) | |
count += 1 | |
print '{0} file(s) copied'.format(count) | |
if __name__ == '__main__': | |
__main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment