Skip to content

Instantly share code, notes, and snippets.

@OzTamir
Created November 29, 2014 10:31
Show Gist options
  • Select an option

  • Save OzTamir/e51c0727fa549f3ddba3 to your computer and use it in GitHub Desktop.

Select an option

Save OzTamir/e51c0727fa549f3ddba3 to your computer and use it in GitHub Desktop.
Copy all the files in the subfolders to main folder
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)
@OzTamir
Copy link
Author

OzTamir commented Nov 29, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment