2

To give credit, the code I am currently working with is from this response by cji, here.

I am trying to recursively pull all files from the source folder, and move them into folders from the file names first-five characters 0:5

My Code Below:

import os
import shutil

srcpath = "SOURCE"
srcfiles = os.listdir(srcpath)

destpath = "DESTINATION"

# extract the three letters from filenames and filter out duplicates
destdirs = list(set([filename[0:5] for filename in srcfiles]))


def create(dirname, destpath):
    full_path = os.path.join(destpath, dirname)
    os.mkdir(full_path)
    return full_path

def move(filename, dirpath):
    shutil.move(os.path.join(srcpath, filename)
                ,dirpath)

# create destination directories and store their names along with full paths
targets = [(folder, create(folder, destpath)) for folder in destdirs]

for dirname, full_path in targets:
    for filename in srcfiles:
        if dirname == filename[0:5]:
            move(filename, full_path)

Now, changing srcfiles = os.listdir(srcpath) and destdirs = list(set([filename[0:5] for filename in srcfiles])) with the code below gets me the paths in one variable and the first five characters of the file names in another.

srcfiles = []
destdirs = []

for root, subFolders, files in os.walk(srcpath):
    for file in files:
       srcfiles.append(os.path.join(root,file))
    for name in files:
       destdirs.append(list(set([name[0:5] for file in srcfiles])))

How would I go about modifying the original code to use this... Or if someone has a better idea on how I would go about doing this. Thanks.

1 Answer 1

5

I can't really test it very easily, but I think this code should work:

import os
import shutil

srcpath = "SOURCE"
destpath = "DESTINATION"

for root, subFolders, files in os.walk(srcpath):
    for file in files:
        subFolder = os.path.join(destpath, file[:5])
        if not os.path.isdir(subFolder):
            os.makedirs(subFolder)
        shutil.move(os.path.join(root, file), subFolder)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the response! 'os.path.mkdir(subFolder):' did not work for me. I had to remove the path. Using: 'os.makedirs(subFolder)'
Sorry about that, I edited my answer. The mkdir and makedirs functions are in the os module, not the os.path submodule. mkdir should work, unless the subfolders are nested more than one level.
if I, say in the future, wanted to elaborate with the sub folders... would that be an easy customization? So Say I had a file called 045679.jpg, I would want it to drop into a 0 Folder > 045 Folder > 04567 Folder or something.
Yes, you could just do this: subFolder = os.path.join(destpath, file[:3], file[:5]), and the os.makedirs() function would create all the folders necessary.
Its funny, right after posting the question, I thought to myself, why not try adding 'file[],' myself. Thanks for all the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.