Search code examples
pythonfilekey-valuestd-pair

Pair files using Python


I have a folder with several .tif files that I would like to pair to perform some functions inside a for loop.

For example:

smp001_GFP.tif

smp001_mCherry.tif (this should be a pair)

smp002_GFP.tif

smp002_mCherry.tif (this another pair)

I would like the for loop to iterate over each pair and perform some functions. For example:

**for** pair **in** folder:
         img_GFP=cv2.imread(pair.__contains__("GFP"))
         img_mCherry=cv2.imread(pair.__contains__("mCherry"))

I've been told that I could pair the files using dictionaries, but which strategy will you recommend to do so?

Thanks!


Solution

  • Here's a different view. Let's assume that the GFP and mCherry parts of the filenames are irrelevant but that the common part is actually that which precedes the underscore.

    If that's the case then:

    from glob import glob
    from os.path import basename, join
    
    DIRECTORY = './tifs' # directory contains the tif files
    result = dict()
     
    for filename in sorted(map(basename, glob(join(DIRECTORY, '*.tif')))):
        key, _ = filename.split('_')
        result.setdefault(key, []).append(filename)
    
    print(result)
    

    Output:

    {'smp002': ['smp002_mCherry.tif', 'smp002_GFP.tif'], 'smp001': ['smp001_mCherry.tif', 'smp001_GFP.tif']}
    

    This gives us a dictionary keyed on the preamble and the "pairs" as a list for each key