繁体   English   中英

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

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

我有一个文件夹,其中包含我已编目的数千张图像,我需要根据它们的部分名称将它们分成文件夹。 名称的每个部分都由“_”分隔。

一个典型的文件名是

DATE_OBJECTCODE_SUBCODE_X_01.jpeg

喜欢

210526 BL RL4_ QRS Eur F699-1-2-2-180 _6702_02_03

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

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

任何帮助将非常感激!

有了这个,您可以使用任何类型的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))

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

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

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

所以你想要的是循环一个带有图像的目录。 对于每个图像,检查是否存在以object_code (例如QRS Eur F699-1-2-2-180 )为名称的文件夹。 如果没有,请创建文件夹。 之后,将图像从当前文件夹(包含所有图像)移动到以object_code为名称的文件夹中。 为此,您可以使用模块os来遍历您的文件并创建新文件夹。

请注意,这假定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