简体   繁体   English

根据文件名的一部分将文件分隔到文件夹中

[英]Segregate files into folders based on part of filename

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 DATE_OBJECTCODE_SUBCODE_X_01.jpeg

like喜欢

210526 BL RL4_ QRS Eur F699-1-2-2-180 _6702_02_03 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.我想根据第二部分(QRS Eur F699-1-2-2-180 或其他任何部分)组织文件,因此该部分具有相应代码的所有文件都将放在一个文件夹中那个标题。

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.我对 python 很陌生,所以我自己尝试了一些代码,但无法弄清楚如何让系统识别文件名的一部分。

Any help would be much appreciated!任何帮助将非常感激!

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:有了这个,您可以使用任何类型的objectcode名称,请注意它不是您还想单独考虑的另一个objectcode名称的一部分:

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 .此代码不是解析文件名,而是在其中查找模式,该模式就是您的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.它适用于任意数量的对象objectcodes ,如果您希望将来以不同的名称重新使用它,这将非常有用。

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.如前所述,如果多个objectcode适合一种模式(我认为不是这种情况),那么您需要应用一些修改。

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).根据您的平台,您可能不需要更改您正在创建的目录的权限(我必须这样做),您还可以将权限修改为可以工作但更严格的东西(现在它只允许一切)。

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.对于每个图像,检查是否存在以object_code (例如QRS Eur F699-1-2-2-180 )为名称的文件夹。 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.之后,将图像从当前文件夹(包含所有图像)移动到以object_code为名称的文件夹中。 For this you can make use of the module os to loop over your files and create new folders.为此,您可以使用模块os来遍历您的文件并创建新文件夹。

Note that this assumes that the object_code is always the second item after splitting the filename on a _.请注意,这假定object_code始终是在 _ 上拆分文件名后的第二项。

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}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM