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!
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 asQRS 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 theobject_code
as name. For this you can make use of the moduleos
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 _.