Created
July 7, 2011 06:12
-
-
Save jonschoning/1068982 to your computer and use it in GitHub Desktop.
move_matches_to_folder2
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) | |
def _get_source_to_dest_filemap(): | |
return [(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))] | |
filemap = _get_source_to_dest_filemap() | |
def _ensure_dest_final_directory(): | |
if not os.path.exists(dest_final_directory): | |
os.makedirs(dest_final_directory) | |
def _process_filemap(): | |
for source_file, dest_file in filemap: | |
os.rename(source_file, dest_file) | |
_print('\n{0} -> {1}'.format(source_file, dest_file)) | |
_increment(count) | |
if filemap: | |
_ensure_dest_final_directory() | |
_process_filemap() | |
_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