I am using django with django-storages and S3 to manage some uploaded files. These files can be uploaded in an "feature" folder, for example:
/uploads/products/product_code/feature/filename.pdf
and I want to move the files out of this feature folder, delete the original file (and eventually the containing folder when all files have been processed) so they can be found at:
/uploads/products/product_code/filename.pdf
I currently have the following code to do this:
def move_files_in_folders(path, folder):
files = default_storage.listdir(path+"/"+folder)
returnable_file_list = list()
for filename in files[1]:
if filename is not '':
movable_file = default_storage.open(path+'/'+folder+'/'+filename)
default_storage.save(path+'/'+filename, movable_file)
default_storage.delete(path+'/'+folder+'/'+filename)
returnable_file_list.append(filename)
default_storage.delete(path + '/' + folder)
return returnable_file_list
This works but it is very slow with long file lists as the system will have to open each file and re-save it.
Is there a way I can refactor this code to avoid the expensive open and save calls?