Created
July 7, 2011 07:07
-
-
Save jonschoning/1069023 to your computer and use it in GitHub Desktop.
move_matches_to_folder3
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,glob | |
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=""): | |
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, os.path.basename(matched_source_file)))) | |
for matched_source_file | |
in glob.glob('{0}/{1}'.format(source_directory, search_pattern))] | |
#process file map | |
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 '{0} -> {1}'.format(source_file, dest_file) | |
count += 1 | |
print '{0} file(s) moved'.format(count) | |
if __name__ == '__main__': | |
__main() | |
#http://paste.pound-python.org/show/9167/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment