I am working on an ArcGIS code where I am looping through multiple images in a folder. I see the files are looping in the project folder (when each image is processed a temporary file comes up but they disappear after that image's processing is complete) but only the last image in the folder saves. I have the save function written in the code but it doesn't actually save. Previously I was able to get this portion of the code to save multiple folders before I added the rename line, but I need the files to contain the original name. Could someone please help me with what I am missing? My code is below:
import arcpy
import os
import glob
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")
env.workspace = r"E:\\GIS\\LoopTest3"
arcpy.env.extent = r"E:\\Quads.shp"
indef_file = "C:\\Users\\kmcnul1\\Desktop\\SnowClassificationScheme2.ecd"
inRasters = glob.glob(r"E:\\LoopImagesTest\\*.tif")
inMaskData = "Quads"
extraction_area = "OUTSIDE"
analysis_extent = "Quads"
for inRaster in inRasters:
outExtractByMask = ExtractByMask(inRaster, inMaskData)
classifiedraster = ClassifyRaster(outExtractByMask, indef_file)
classifiedraster.save = (r"E:\\GIS\\LoopTest3")
newname = inRaster + "CF"
arcpy.management.Rename(classifiedraster,newname)
What I want to add after is:
ClassRasts = glob.glob(r"E:\\GIS\\LoopTesLoop3\\*.crf")
for ClassRast in ClassRasts:
outCellstats = CellStatistics(ClassRast, "SUM")
outCellstats.save(r"E:\\GIS\\LoopTest3")
arcpy.conversion.ExportTable("FinalCellStat", r"E:\\GIS\\LoopTest3 '', "NOT_USE_ALIAS", 'VALUE "VALUE" true true false 0 Long 0 0,First,#,FinalCellStat,VALUE,-1,-1;COUNT "COUNT" true true false 0 Double 0 0,First,#,FinalCellStat,COUNT,-1,-1', None)
arcpy.conversion.TableToExcel("FinalCellStat_ExportTable", r"E:\\GIS\\LoopTest3\\*.xls", "ALIAS", "CODE")
But I need more than one file folder to save. Ideally, I'd have the last chunk in this loop too, but I was having the same issue with the Cell Statistics only saving the last file in the loop
It would help if you formatted your code in here since there are some missing or unmatched ' and " in the ExportTable. Click the ..., then the </> to get the code window.
If you don't give it a new name in each iteration, you'll just overwrite the name that you have in there.
I suspect it's the
r"E:\\GIS\\LoopTest3\\*.xls"
output.
Try this to get you started.
cnt = 1
for ClassRast in ClassRasts:
outputName = f'{os.path.basename(ClassRast)}_{cnt}'
outCellstats = CellStatistics(ClassRast, "SUM")
outCellstats.save(r"E:\\GIS\\LoopTest3")
# TODO: FIX THIS METHOD r"E:\\GIS\\LoopTest3 '' ??
arcpy.conversion.ExportTable("FinalCellStat", r"E:\\GIS\\LoopTest3 '', "NOT_USE_ALIAS", 'VALUE "VALUE" true true false 0 Long 0 0,First,#,FinalCellStat,VALUE,-1,-1;COUNT "
COUNT " true true false 0 Double 0 0,First,#,FinalCellStat,COUNT,-1,-1', None)
arcpy.conversion.TableToExcel("FinalCellStat_ExportTable", fr"E:\\GIS\\LoopTest3\\{outputName}.xls", "ALIAS", "CODE")
cnt=+1
Thank you, maybe I should not have added the second part, as I cannot get the first part to save correctly. I need help getting all the files to save in this part of the loop
import arcpy
import os
import glob
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")
env.workspace = r"E:\\GIS\\LoopTest3"
arcpy.env.extent = r"E:\\Quads.shp"
indef_file = "C:\\Users\\kmcnul1\\Desktop\\SnowClassificationScheme2.ecd"
inRasters = glob.glob(r"E:\\LoopImagesTest\\*.tif")
inMaskData = "Quads"
extraction_area = "OUTSIDE"
analysis_extent = "Quads"
for inRaster in inRasters:
outExtractByMask = ExtractByMask(inRaster, inMaskData)
classifiedraster = ClassifyRaster(outExtractByMask, indef_file)
classifiedraster.save = (r"E:\\GIS\\LoopTest3")
newname = inRaster + "CF"
arcpy.management.Rename(classifiedraster,newname)
Only the last file in the inRasters folder saves, while the others loop through but disappear.