I have a list of 480 files and I would like to save them to 80 folders based on their filenames. Examples of the filenames are:
LT50300281984137PAC00_sr_band1.tif
LT50300281984137PAC00_sr_band2.tif
LT50300282007136PAC01_sr_band1.tif
LT50300282007136PAC01_sr_band2.tif
LT50300282002138LGS01_sr_band1.tif
LT50300282002138LGS01_sr_band2.tif
I want to save files that have matching characters in a slice of the first 21 characters [0:21] into folders that have that same name. For example:
LT50300281984137PAC00_sr_band1.tif
LT50300281984137PAC00_sr_band2.tif
would go into folder titled LT50300281984137PAC00
and
LT50300282007136PAC01_sr_band1.tif
LT50300282007136PAC01_sr_band2.tif
would go into folder titled LT50300282007136PAC01
I have already created the folders using this code:
import arcpy
import os
#pathway where there are only 80 .tif files, one for each landsat scene
arcpy.env.workspace=r'F:\Sheyenne\Atmospherically Corrected Landsat\hank_masked\Band 1'
#list of rasters in above pathway
list1 = arcpy.ListRasters("*.tif")
#output save pathway
mainpath=r'F:\Sheyenne\Atmospherically Corrected Landsat\Individual Scenes\Main'
#create folder for each landsatscene containing first 21 characters
for raster in list1:
rasterName=raster[0:21]
if raster.startswith(rasterName.split("_")[0]):
final_path=os.path.join(mainpath,rasterName)
os.makedirs(final_path)
and now I want to take every file that has 'band' in the name like I showed above and store it in the correct folders but this is where I am stuck