Segregate files into folders based on part of filename

1.5k views Asked by At

I've a folder containing thousands of images that I've catalogued, and I need to separate them into folders based on part of their name. Each section of a name is separated by '_'.

A typical file name is

DATE_OBJECTCODE_SUBCODE_X_01.jpeg

like

210526 BL RL4_QRS Eur F699-1-2-2-180_6702_02_03

I'd like to organise the file based on the second section, (the QRS Eur F699-1-2-2-180 or whatever that part is), so all files with corresponding codes for that section will be placed in a folder with that title.

I'm quite new to python so I've tried a few codes myself but haven't been able to figure out how to make the system recognise part of a filename.

Any help would be much appreciated!

2

There are 2 answers

1
sandertjuh On

So what you want is to loop over a directory with images. For each image, check if there exists a folder with the object_code (such as QRS Eur F699-1-2-2-180) as name. If not, create the folder. After that, move the image from the current folder (with all the images) to the folder with the object_code as name. For this you can make use of the module os to loop over your files and create new folders.

Note that this assumes that the object_code is always the second item after splitting the filename on a _.

path_images = 'path/to/my/images'

for image in os.listdir(path_images):
    if image.endswith('.png'):
        object_code = image.split("_")[1]  # object_code is something like QRS Eur F699-1-2-2-180
        
        if not path.isdir(object_code):  # Folder with this object_code does not yet exist, create it
            os.mkdir(object_code)
            
        # Move the file to the folder with the object_code name
        Path(f"{path_images}/{image}").rename(f"{path_images}/{object_code}/{image}")
0
atru On

With this you can use any type of objectcode name, just be careful that it is not part of another objectcode name that you also want to consider separately:

import os, glob

# This assumes that this code is run inside
# the directory with the image files

# Path to the directory you will store 
# the directories with files (current here) 
path_to_files = os.getcwd()
objectcodes = ['QQS Eur F699-1', 'BL RL4_QRS Eur F699-1-2-7-5_379' ];

# For each objectcode
for obc in objectcodes:
    # Make a directory if it doesn't already exist
    file_dir = os.path.join(path_to_files, obc)
    if not os.path.isdir(file_dir):
        os.mkdir(file_dir)
        # If you need to set the permissions 
        # (this enables it all - may not be what you want)
        os.chmod(file_dir, 0o777)

    # Then move all the files that have that objectcode 
    # in them and end with *.jpeg to the correct dir
    for fname in glob.glob('*' + obc + '*.jpg'):
        # Move the file
        os.rename(fname, os.path.join(file_dir, fname))

Instead of parsing the filenames, this code looks for a pattern in them, and that pattern is your objectcode. It runs for an arbitrary number of objectcodes, which makes it useful if you expect to re-use it in the future with different names.

As stated earlier, if more than one objectcode fit a pattern (which I assumed is not the case), then you need to apply some modifications.

Depending on your platform you may not need to change the permissions of the directory you are creating (I had to), and you may also modify the permissions to something working but more strict (now it just allows everything).