Created
July 7, 2011 05:51
-
-
Save jonschoning/1068952 to your computer and use it in GitHub Desktop.
move_matches_to_folder
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
def __main(): | |
kwargs={ | |
'source_directory' :r'D:/bin/completeddownloads/docs' | |
,'search_pattern' :'*Success*' | |
,'dest_directory_root' :r'D:/Users/qwerty/Documents/_user/Programming/Reference' | |
,'dest_leaf_directory' :'Success' | |
} | |
move_matches_to_folder(**kwargs) | |
def move_matches_to_folder(source_directory, search_pattern, dest_directory_root, dest_leaf_directory=""): | |
import sys,os,glob | |
def _increment(x): x[0]+=1 | |
def _get_filename(filepath): return os.path.split(filepath)[1] | |
_print=sys.stdout.write | |
count=[0] | |
#normalize paths | |
dest_final_directory=os.path.abspath('{0}/{1}'.format(dest_directory_root, dest_leaf_directory)) | |
source_directory=os.path.abspath(source_directory) | |
#find matching files and build final paths | |
source_to_dest_filemap= \ | |
[(os.path.abspath(matched_source_file), | |
os.path.abspath('{0}/{1}'.format(dest_final_directory, _get_filename(matched_source_file)))) | |
for matched_source_file | |
in glob.glob('{0}/{1}'.format(source_directory, search_pattern))] | |
if source_to_dest_filemap: | |
if not os.path.exists(dest_final_directory): | |
os.makedirs(dest_final_directory) | |
for source_file, dest_file in source_to_dest_filemap: | |
os.rename(source_file, dest_file) | |
_print('\n{0} -> {1}'.format(source_file, dest_file)) | |
_increment(count) | |
_print('\n{0} file(s) moved\n'.format(count[0])) | |
if __name__ == '__main__': | |
__main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment