Background:

Recently, I had a hierarchy of files/images in the following format…

./
    a/
        res/
            drawable-mdpi/ic_name_a.png
            drawable-hdpi/ic_name_a.png
            drawable-xhdpi-v21/ic_name_a.png
    b/
        res/
            drawable-mdpi/ic_name_b.png
            drawable-hdpi/ic_name_b.png
            drawable-xhdpi-v21/ic_name_b.png
    c/
        res/
            drawable-mdpi/ic_name_c.png
            drawable-hdpi/ic_name_c.png
            drawable-xhdpi-v21/ic_name_c.png

But, I wanted to combine all of those sub-directories together to look like the following…

./
    res/
        drawable-mdpi/
            ic_name_a.png
            ic_name_b.png
            ic_name_c.png
        drawable-hdpi/
            ic_name_a.png
            ic_name_b.png
            ic_name_c.png
        drawable-xhdpi-v21/
            ic_name_a.png
            ic_name_b.png
            ic_name_c.png

In order to accomplish this task of combining similar sub-directories from different parent folders, I decided to practice some quick Python code scripting. You can find the code in it’s entirety along with some comments: bubble_up_subdirectories.py on GitHub

And, here quickly is the main method of interest to accomplish this file-moving solution:

def bubble_up_subdirectories(top, is_copy=True, depth=0):
    if IS_SHOW_DEBUG_LOG:
        print "bubble_up_subdirectories(top=" + top + ")"
    for root, dirs, files in os.walk(top):
        for name in dirs:
            bubble_up_subdirectories(os.path.join(root, name), is_copy, depth + 1)
        for name in files:
            if (depth != 0):
                src = os.path.join(root, name)
                if IS_SHOW_DEBUG_LOG:
                    print 'src=' + src
                dst = src[src.index(os.path.sep) + 1:]
                dst = dst[dst.index(os.path.sep) + 1:dst.rindex(os.path.sep)]
                if IS_SHOW_DEBUG_LOG:
                    print 'dst=' + dst
                if not os.path.exists(dst):
                    os.makedirs(dst)
                if is_copy:
                    shutil.copy(src, dst)
                else:
                    shutil.move(src, dst)


bubble_up_subdirectories(DIRECTORY_FROM)
Now a quick word from a tech company who helps support me and this site: