Recursively move files from subdirectory to folders in parent directory
Question: In the following directory,

    
    
    /Drive/Company/images/full_res/
    

there exists over 900 .jpg files, like so:

    
    
    Skywalker.jpg
    Pineapple.jpg
    Purple.jpg
    White.jpg
    

One level up from 'full_res' ('images'), there exists nearly the same amount
of folders as there are images in 'full_res', and for the most part are named
accordingly, like so:

    
    
    ..
    .
    Skywalker/
    Pineapple/
    Purple/
    White/
    full_res/
    

I need to move or copy all of the files in full_res to their correspondingly
named folder in 'images' while simultaneously renaming the file to
'export.jpg'. The result should be as such:

    
    
    /Drive/Company/images/
    ----------------------
    ..
    .
    Skywalker/export.jpg
    Pineapple/export.jpg
    Purple/export.jpg
    White/export.jpg
    

[This is the closest thing](http://stackoverflow.com/questions/18295056/batch-
file-to-recursively-move-files-to-first-level-directory-from-deeper-subdir) I
could find relevant to my query (I think?), but I'm looking for a way to do
this with Python. Here's the nothing I was able to produce:

    
    
    import os, shutil
    
    path = os.path.expanduser('~/Drive/Company/images/')
    src = os.listdir(os.path.join(path, 'full_res/'))
    
    for filename in src:
        images = [filename.endswith('.jpg') for filename in src]
        for x in images:
            x = x.split('.')
            print x[0] #log to console so I can see it's at least doing something (it's not)
            dest = os.path.join(path, x[0])
            if not os.path.exists(dest):
                os.makedirs(dest) #create the folder if it doesn't exist
            shutil.copyfile(filename, os.path.join(dest, '/export.jpg'))
    

* * *

There's probably a lot wrong with this, but I suspect one of my biggest
failings has something to do with my misunderstanding of the concept of list
comprehension. In any case, I've been struggling with this for so long that I
probably could have manually moved and renamed all of those image files myself
by now. Any and all help is appreciated.


Answer: You're not far from the correct answer:

    
    
    import os, shutil
    
    path = os.path.expanduser('~/Drive/Company/images/')
    src = os.listdir(os.path.join(path, 'full_res'))
    
    for filename in src:
        if filename.endswith('.jpg'):
            basename = os.path.splitext(filename)[0]
            print basename #log to console so I can see it's at least doing something (it's not)
            dest = os.path.join(path, basename)
            if not os.path.exists(dest):
                os.makedirs(dest) #create the folder if it doesn't exist
            shutil.copyfile(os.path.join(path, 'full_res', filename), os.path.join(dest, 'export.jpg'))
    

