[GIS] Selecting files with matching names and then executing a process

arcpypython

I have two lists and I would like to find a way to select them based on names.

In list one I have a series of tif files:

list1=['LT50300281984137PAC00_sr_band1.tif',
,'LT50300281984137PAC00_sr_band2.tif'  
'LT50300281984137PAC00_sr_band3.tif','LT50300281994260XXX03_sr_band1.tif',
'LT50300281994260XXX03_sr_band2.tif',
'LT50300281994260XXX03_sr_band3.tif']

in list two I have two files:

list2=[LT50300281984137PAC00_mask.tif,LT50300281994260XXX03_mask.tif]

I want to exectue a process (which I won't ask how to do here) for files in list one which start with LT50300281984137PAC00 to the file in list 2 which starts the same way, and the same for the files which start with LT50300281994260XXX03.

I think it will go something like:

#select the files based on names
for i in list1:
    if i[0:20] in list2:
#this code executes the process I want ONLY if the naming matches
        for b in list1:
            arcpy.gp.ExtractByMask_sa(b,list2,outraster)
     else:
          print('Done')

So basically if the file names in list 1 match those in list 2 I want to execute my process otherwise print done

Edit:

New code being tried is:

import arcpy

arcpy.env.workspace=r'F:\Sheyenne\Atmospherically Corrected Landsat\hank_practice'

list1 = arcpy.ListRasters("*band*")

list2 = arcpy.ListRasters("*_nocloud.tif")  

for raster in list1:
  rasterName = raster
  rasterMask = "" #initialize your variable to be populated in the inner loop
  for mask in list2:
    if mask.startswith(rasterName):
      rasterMask = mask
      break #break out of the inner loop - we have a match
  if rasterMask != "": #just to make sure we have something
    #generic naming
    outraster = rasterName.replace(".tif", "_masked.tif")
    arcpy.gp.ExtractByMask_sa(rasterName,rasterMask,outraster)

Best Answer

I thought about this and you might have luck with a loop over your primarily list and an inner loop looking for its mask.

How about trying something like this:

for raster in list1:
  rasterName = raster
  rasterMask = "" #initialize your variable to be populated in the inner loop
  for mask in list2:
    if mask.startswith(rasterName.split("_")[0]):
      rasterMask = mask
      break #break out of the inner loop - we have a match
  if rasterMask != "": #just to make sure we have something
    #generic naming
    outraster = rasterName.replace(".tif", "_masked.tif")
    arcpy.gp.ExtractByMask_sa(rasterName,rasterMask,outraster)

Edit... I thought on the outraster part, you can just replace part of the input's name. Typically, I take the extension and replace it with "_somethingToKnowItIsAProduct" and add the extension back in. So, the .replace(".tif", "_masked.tif) would mark your output like LT50300281984137PAC00_sr_band1_masked.tif.