Created
November 29, 2014 10:31
-
-
Save OzTamir/e51c0727fa549f3ddba3 to your computer and use it in GitHub Desktop.
Copy all the files in the subfolders to main folder
This file contains hidden or 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 shutil | |
| import os | |
| # The current working directory | |
| dest_dir = os.getcwd() | |
| # The generator that walks over the folder tree | |
| walker = os.walk(dest_dir) | |
| # the first walk would be the same main directory | |
| # which if processed, is | |
| # redundant | |
| # and raises shutil.Error | |
| # as the file already exists | |
| rem_dirs = walker.next()[1] | |
| for data in walker: | |
| for files in data[2]: | |
| try: | |
| shutil.move(data[0] + os.sep + files, dest_dir) | |
| except shutil.Error: | |
| print 'Error. \n Folder: %s\n File: %s' % (str(data[0]), str(files)) | |
| # still to be on the safe side | |
| continue | |
| # clearing the directories | |
| # from whom we have just removed the files | |
| for dirs in rem_dirs: | |
| shutil.rmtree(dest_dir + os.sep + dirs) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Taken from here:
https://techmyway.wordpress.com/2012/01/03/python-os-and-shutil-module-usage/