<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Only Last File in Loop is Saving in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/only-last-file-in-loop-is-saving/m-p/1254169#M66762</link>
    <description>&lt;P&gt;Only the last file in the inRasters folder saves, while the others loop through but disappear.&lt;/P&gt;</description>
    <pubDate>Thu, 02 Feb 2023 00:20:24 GMT</pubDate>
    <dc:creator>KatherineMcNulty</dc:creator>
    <dc:date>2023-02-02T00:20:24Z</dc:date>
    <item>
      <title>Only Last File in Loop is Saving</title>
      <link>https://community.esri.com/t5/python-questions/only-last-file-in-loop-is-saving/m-p/1253073#M66715</link>
      <description>&lt;P&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;import os&lt;BR /&gt;import glob&lt;BR /&gt;from arcpy import env&lt;BR /&gt;from arcpy.sa import *&lt;/P&gt;&lt;P&gt;arcpy.CheckOutExtension("Spatial")&lt;/P&gt;&lt;P&gt;env.workspace = r"E:\\GIS\\LoopTest3"&lt;BR /&gt;arcpy.env.extent = r"E:\\Quads.shp"&lt;BR /&gt;indef_file = "C:\\Users\\kmcnul1\\Desktop\\SnowClassificationScheme2.ecd"&lt;/P&gt;&lt;P&gt;inRasters = glob.glob(r"E:\\LoopImagesTest\\*.tif")&lt;/P&gt;&lt;P&gt;inMaskData = "Quads"&lt;BR /&gt;extraction_area = "OUTSIDE"&lt;BR /&gt;analysis_extent = "Quads"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for inRaster in inRasters:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;outExtractByMask = ExtractByMask(inRaster, inMaskData)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;classifiedraster = ClassifyRaster(outExtractByMask, indef_file)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;classifiedraster.save = (r"E:\\GIS\\LoopTest3")&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;newname = inRaster + "CF"&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;arcpy.management.Rename(classifiedraster,newname)&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want to add after is:&lt;/P&gt;&lt;P&gt;ClassRasts = glob.glob(r"E:\\GIS\\LoopTesLoop3\\*.crf")&lt;/P&gt;&lt;P&gt;for ClassRast in ClassRasts:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;outCellstats = CellStatistics(ClassRast, "SUM")&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;outCellstats.save(r"E:\\GIS\\LoopTest3")&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;arcpy.conversion.ExportTable("FinalCellStat",&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 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)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;arcpy.conversion.TableToExcel("FinalCellStat_ExportTable", r"E:\\GIS\\LoopTest3\\*.xls", "ALIAS", "CODE")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jan 2023 21:07:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/only-last-file-in-loop-is-saving/m-p/1253073#M66715</guid>
      <dc:creator>KatherineMcNulty</dc:creator>
      <dc:date>2023-01-30T21:07:08Z</dc:date>
    </item>
    <item>
      <title>Re: Only Last File in Loop is Saving</title>
      <link>https://community.esri.com/t5/python-questions/only-last-file-in-loop-is-saving/m-p/1253088#M66716</link>
      <description>&lt;P&gt;It would help if you formatted your code in here since there are some missing or unmatched ' and " in the ExportTable.&amp;nbsp; Click the ..., then the &amp;lt;/&amp;gt; to get the code window.&lt;/P&gt;&lt;P&gt;If you don't give it a new name in each iteration, you'll just overwrite the name that you have in there.&lt;/P&gt;&lt;P&gt;I suspect it's the&lt;/P&gt;&lt;LI-CODE lang="python"&gt;r"E:\\GIS\\LoopTest3\\*.xls"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;output.&lt;/P&gt;&lt;P&gt;Try this to get you started.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jan 2023 21:46:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/only-last-file-in-loop-is-saving/m-p/1253088#M66716</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-01-30T21:46:12Z</dc:date>
    </item>
    <item>
      <title>Re: Only Last File in Loop is Saving</title>
      <link>https://community.esri.com/t5/python-questions/only-last-file-in-loop-is-saving/m-p/1254166#M66761</link>
      <description>&lt;PRE&gt;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&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;import arcpy&lt;BR /&gt;import os&lt;BR /&gt;import glob&lt;BR /&gt;from arcpy import env&lt;BR /&gt;from arcpy.sa import *&lt;BR /&gt;&lt;BR /&gt;arcpy.CheckOutExtension("Spatial")&lt;BR /&gt;&lt;BR /&gt;env.workspace = r"E:\\GIS\\LoopTest3"&lt;BR /&gt;arcpy.env.extent = r"E:\\Quads.shp"&lt;BR /&gt;indef_file = "C:\\Users\\kmcnul1\\Desktop\\SnowClassificationScheme2.ecd"&lt;BR /&gt;&lt;BR /&gt;inRasters = glob.glob(r"E:\\LoopImagesTest\\*.tif")&lt;BR /&gt;&lt;BR /&gt;inMaskData = "Quads"&lt;BR /&gt;extraction_area = "OUTSIDE"&lt;BR /&gt;analysis_extent = "Quads"&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;for inRaster in inRasters:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;outExtractByMask = ExtractByMask(inRaster, inMaskData)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;classifiedraster = ClassifyRaster(outExtractByMask, indef_file)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;classifiedraster.save = (r"E:\\GIS\\LoopTest3")&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;newname = inRaster + "CF"&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;arcpy.management.Rename(classifiedraster,newname)&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Feb 2023 00:16:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/only-last-file-in-loop-is-saving/m-p/1254166#M66761</guid>
      <dc:creator>KatherineMcNulty</dc:creator>
      <dc:date>2023-02-02T00:16:27Z</dc:date>
    </item>
    <item>
      <title>Re: Only Last File in Loop is Saving</title>
      <link>https://community.esri.com/t5/python-questions/only-last-file-in-loop-is-saving/m-p/1254169#M66762</link>
      <description>&lt;P&gt;Only the last file in the inRasters folder saves, while the others loop through but disappear.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2023 00:20:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/only-last-file-in-loop-is-saving/m-p/1254169#M66762</guid>
      <dc:creator>KatherineMcNulty</dc:creator>
      <dc:date>2023-02-02T00:20:24Z</dc:date>
    </item>
  </channel>
</rss>

