如何对相关的 .tif 文件进行分组?

How to group related .tif files?

我正在尝试对与 .tif 图像关联的相关文件进行分组。从列表中可以看出,每组有 7 个相关文件。我正在寻找一种方法来对这些文件进行分组,以便我可以通过 shutil.move() 将它们移动到不同的文件夹中。

下面的脚本是我根据this answer尝试失败的。结果与输入的 files 列表相同。

我如何调整它来执行我想要的分组?

import os, itertools

files = ['F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511759_sw.tfw',
       'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511759_sw.tif',
       'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511759_sw.tif.aux.xml',
       'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511759_sw.tif.ovr',
       'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511759_sw.tif.vat.cpg',
       'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511759_sw.tif.vat.dbf',
       'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511759_sw.tif.xml',
       'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511760_sw.tfw',
       'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511760_sw.tif',
       'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511760_sw.tif.aux.xml',
       'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511760_sw.tif.ovr',
       'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511760_sw.tif.vat.cpg',
       'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511760_sw.tif.vat.dbf',
       'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511760_sw.tif.xml']

test = sorted(files)
grouped = [list(g) for _, g in itertools.groupby(test, lambda x: x.split('_')[1])]

预期输出:

[['F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511759_sw.tfw',
           'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511759_sw.tif',
           'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511759_sw.tif.aux.xml',
           'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511759_sw.tif.ovr',
           'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511759_sw.tif.vat.cpg',
           'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511759_sw.tif.vat.dbf',
           'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511759_sw.tif.xml'],
           ['F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511760_sw.tfw',
           'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511760_sw.tif',
           'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511760_sw.tif.aux.xml',
           'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511760_sw.tif.ovr',
           'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511760_sw.tif.vat.cpg',
           'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511760_sw.tif.vat.dbf',
           'F:\juniper_project\data\raster\deliverables\OR\reclass\reclass_4511760_sw.tif.xml']]

关闭! split list上的索引是1,应该是2。

尝试

grouped = [list(g) for _, g in itertools.groupby(test, lambda x: x.split('_')[2])]