<?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: IndexError: list index out of range PLEASE HELP in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/indexerror-list-index-out-of-range-please-help/m-p/651956#M50766</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ok. I think I ruined it by running it once with my part numbers incorrect.&amp;nbsp; Then I changed them, but it had already incorrectly renamed one output file with just two parts, so that would cause this error.&amp;nbsp; Thanks.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 19 Jun 2014 18:53:08 GMT</pubDate>
    <dc:creator>GrantWest</dc:creator>
    <dc:date>2014-06-19T18:53:08Z</dc:date>
    <item>
      <title>IndexError: list index out of range PLEASE HELP</title>
      <link>https://community.esri.com/t5/python-questions/indexerror-list-index-out-of-range-please-help/m-p/651954#M50764</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Trying to execute the following script tool to rename all the rasters in a folder to a shorter, but still uniquely identifying, version of the names they already have.&amp;nbsp; I am getting "IndexError: list index out of range", "Failed to execute (Rename).&amp;nbsp; I can't figure out what I need to change for this to work.&amp;nbsp; Any help is appreciated, thanks.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import os&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;from arcpy import env&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;RASTER_DIR = 'C:/GIS_Working/QQ/Temp_monthly_means_gridded/'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;env.workspace = RASTER_DIR&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;rasters = arcpy.ListRasters()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;for raster in rasters:&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fileName, fileExtension = os.path.splitext(raster)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fileNameParts = fileName.split('_')&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; compactFileName = fileNameParts[1] + fileNameParts[4] + fileExtension&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Rename_management(raster, compactFileName)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Jun 2014 18:35:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/indexerror-list-index-out-of-range-please-help/m-p/651954#M50764</guid>
      <dc:creator>GrantWest</dc:creator>
      <dc:date>2014-06-19T18:35:00Z</dc:date>
    </item>
    <item>
      <title>Re: IndexError: list index out of range PLEASE HELP</title>
      <link>https://community.esri.com/t5/python-questions/indexerror-list-index-out-of-range-please-help/m-p/651955#M50765</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If the split creates less than 5 items in its list, the index out of range will occur.&amp;nbsp; Since you have altered files in the same directory they no longer follow the pattern, so you can only run the tool once when all the Rasters have the full name.&amp;nbsp; You should check for len(fileNameParts) = 5 before renaming the file.&amp;nbsp; The list indexes are 0 based, so the 5th list item = index 4.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import os
import arcpy
from arcpy import env
RASTER_DIR = 'C:/GIS_Working/QQ/Temp_monthly_means_gridded/'
env.workspace = RASTER_DIR

rasters = arcpy.ListRasters()

for raster in rasters:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; fileName, fileExtension = os.path.splitext(raster)
&amp;nbsp;&amp;nbsp;&amp;nbsp; fileNameParts = fileName.split('_')
&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(fileNameParts) &amp;gt;= 5:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; compactFileName = fileNameParts[1] + fileNameParts[4] + fileExtension
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Rename_management(raster, compactFileName)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 03:38:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/indexerror-list-index-out-of-range-please-help/m-p/651955#M50765</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2021-12-12T03:38:36Z</dc:date>
    </item>
    <item>
      <title>Re: IndexError: list index out of range PLEASE HELP</title>
      <link>https://community.esri.com/t5/python-questions/indexerror-list-index-out-of-range-please-help/m-p/651956#M50766</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ok. I think I ruined it by running it once with my part numbers incorrect.&amp;nbsp; Then I changed them, but it had already incorrectly renamed one output file with just two parts, so that would cause this error.&amp;nbsp; Thanks.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Jun 2014 18:53:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/indexerror-list-index-out-of-range-please-help/m-p/651956#M50766</guid>
      <dc:creator>GrantWest</dc:creator>
      <dc:date>2014-06-19T18:53:08Z</dc:date>
    </item>
  </channel>
</rss>

