【问题标题】:Segregate files into folders based on part of filename根据文件名的一部分将文件分隔到文件夹中
【发布时间】:2021-06-11 16:40:00
【问题描述】:

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

典型的文件名是

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

任何帮助将不胜感激!

【问题讨论】:

  • 您能否提供更多文件名示例
  • 我们需要了解有关命名约定的更多细节——比如所有文件是否都有 QRS 部分,它们是否都以三组数字结尾,并用下划线分隔。
  • 文件名的一些其他示例:210526 BL RL4_QRS Eur D656 - 1835_12.jpeg 210526 BL RL4_QRS Eur F699-1-2-2-185_6972_X_01.jpeg 210526 BL RL4_QRS Eur F699-1-7 -5_379_01.jpeg 210526 BL RL4_QRS Eur F699-2-2-2-3_Jun-Dec1957_Fasc1_Folios1-42_i_01.jpeg
  • 总而言之,在这种情况下,它们都有 QRS,但它后面的代码会有所不同。不过,我将来可能会有一些没有 QRS 的文件,所以我需要一些能识别下划线的东西。所以 A_B_C_01.jpeg 将进入一个名为“B”的文件夹

标签: python list file namespace-organisation


【解决方案1】:

有了这个,您可以使用任何类型的 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 符合一种模式(我认为不是这种情况),那么您需要应用一些修改。

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

【讨论】:

    【解决方案2】:

    所以你想要的是循环一个带有图像的目录。对于每个图像,检查是否存在名称为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}")
    

    【讨论】:

    • 谢谢你!我刚刚尝试过,我在最后一行得到了无效的语法。知道那可能是什么吗?
    猜你喜欢
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    相关资源
    最近更新 更多