List Index Out of Range

3262
7
09-18-2012 10:45 AM
MarilynGambone
New Contributor
Hi,

I have a batch processing script for copying multiple shapefiles in a folder into a feature dataset in a file geodatabase.  It's different from the built-in ArcToolBox in that I have to name each input feature class differently from the original names.  So I created a separator in order to split and derive the output feature class name.  The script works when I'm debugging (substituting the argv[..] variables with actual values.  But when I add the script to my Toolbox, I keep getting the error: list index is out of range.  I appreciate any assistance.  The script is as follows:

#---------------------------------------------------------------------------------------
#Script Name: BatchCopyRename.py
#Script Path:
#Created by: Marilyn Gambone
#Date:       9/18/2012
#E-mail: mgambone@brc.tamus.edu

#---------------------------------------------------------------------------------------

#Purpose:
# To batch copy multiple shapefiles in a folder into a file geodatabase feature dataset.

#Deployment
# Attach this script to an ArcToolBox.
# Developed in ArcGIS Desktop license version 10.0.
# Desktop computer Toolbox path:

#Properties
#Name           BatchCopyRename
#Label           Batch Copy and Rename Feature Classes
#Description   Copy multiple Feature Classes from a folder into a file geodatabase
#                   feature dataset and rename each output feature class.

#Parameter List
#           Parameter Properties
#             Display Text                            Data Type                              Type         Direction    MultiValue
# argv[0]  Select Input Features               Feature Layer                           Required   Input         Yes
# argv[1]  Select Output Feature Dataset   Workspace or Feature Dataset    Required   Input         No
# argv[2]  Separator                                String                                     Optional    Input         No
# argv[3]  Output Geodatabase                 Workspace or Feature Dataset   Derived     Output       No



#Import system modules
import ConversionUtils, time

ConversionUtils.gp.Overwriteoutput = 1

#Define message constants so they may be translated easily
msgErrorInvalidOutPath = ConversionUtils.gp.GetIDMessage(86127) #"Output path does not exist"
msgSuccess = ConversionUtils.gp.GetIDMessage(86128) #" successfully converted to "
msgFailed = ConversionUtils.gp.GetIDMessage(86129) # "Failed to convert "


#Set script arguments
# Argument 1 is the list of feature classes to be converted
inFeatureClasses = ConversionUtils.gp.GetParameterAsText(0)

# The list is split by semicolons ";"
inFeatureClasses = ConversionUtils.SplitMultiInputs(inFeatureClasses)

# The output workspace where the shapefiles are created
outWorkspace = ConversionUtils.gp.GetParameterAsText(1)

# Get the separator ('_')
sep = ConversionUtils.gp.GetParameterAsText(2)

# Set the destination workspace parameter (which is the same value as the output workspace)
# the purpose of this parameter is to allow connectivity in Model Builder.
ConversionUtils.gp.SetParameterAsText(3,outWorkspace)

# Set the progressor
#Message "Converting multiple feature classes ..."
ConversionUtils.gp.SetProgressor("step",ConversionUtils.gp.GetIDMessage(86145) , 0, len(inFeatureClasses))

#Loop through the list of input feature classes and convert/copy to feature dataset
for inFeatureClass in inFeatureClasses:
  try:
    # Set the progressor label
    #Message "Converting.."
    ConversionUtils.gp.SetProgressorLabel(ConversionUtils.gp.GetIDMessage(86130) + inFeatureClass)

    # Generate a valid output name
    outFeatureClass = ConversionUtils.GenerateOutputName(inFeatureClass, outWorkspace)
    fcSplit = outFeatureClass.split(sep)
    fcName = fcSplit[2]
    outDataset = outWorkspace + "\\" + fcName.upper()

    # Execute Copy/Convert the inFeatureClasses to the outFeatureClasses
    ConversionUtils.CopyFeatures(inFeatureClass, outDataset)

    # If the Copy/Convert was successfull add a message stating this
    ConversionUtils.gp.AddMessage("%s %s %s" % (inFeatureClasses, msgSuccess, outFeatureClass))

  except Exception, ErrorDesc:
    # Except block for the loop. If the tool fails to convert one of the feature classes, it will come into this block
    #  and add warnings to the messages, then proceed to attempt to convert the next input feature class.
    msgWarning = msgFailed + "%s" % inFeatureClass
    msgStr = ConversionUtils.gp.GetMessages(2)
    ConversionUtils.gp.AddWarning(ConversionUtils.ExceptionMessages(msgWarning, msgStr, ErrorDesc))

time.sleep(0.5)
Tags (2)
0 Kudos
7 Replies
ArkadiuszMatoszka
Occasional Contributor II
Can you post input parameters and exception you get?
At the moment I would guess problem in this part:

fcSplit = outFeatureClass.split(sep)
fcName = fcSplit[2]


If sep doesn't appear in outFeatureClass there is no index [2] for fcSplit.

Regards
Arek
0 Kudos
MarilynGambone
New Contributor
Hi,

No... that doesn't seem to be the problem because it's running okay as long as I have actual values to my arguments:

#Set workspace   Testing Values
env.workspace =  r"K:\FGDBIntermediate\Soil\SSURGO2_2\soilmu_a_states_dis\DE"  #sys.argv[0]
inFolder =           r"K:\FGDBIntermediate\Soil\SSURGO2_2\soilmu_a_states_dis\DE"  #sys.argv[0]
inFtrDs =            r"K:\FGDBIntermediate\Soil\SSURGO1.gdb\DE"  #sys.argv[1]
sep =                 '_' #sys.argv[02]

When I run the script in Debug mode in ArcToolbox, I get this error:

"ERROR 000714: Error in script BatchCopyRename.
Error in executing: cmd.exe /C C:\Python26\ArcGIS10.0\pythonw.exe C:\PYTHON~2\BATCHC~2.PY

Failed to execute (BatchCopyRename)."

Something must have gotten wrong with my PythonWin.  I don't see the IDLE anymore.  I could yesterday...I re-installed it and I can't get the IDLE back.  My PythonWin version is 2.6 for ArcGIS10.0 Desktop.
0 Kudos
MarilynGambone
New Contributor
Can you post input parameters and exception you get?
At the moment I would guess problem in this part:

fcSplit = outFeatureClass.split(sep)
fcName = fcSplit[2]


If sep doesn't appear in outFeatureClass there is no index [2] for fcSplit.

Regards
Arek


Hi,  sorry....I sent the wrong values.  My testing values are:

For the list:
Failed to convert: K:\FGDBIntermediate\Soil\SSURGO2_2\soilmu_a_states_dis\DE\soilmu_a_de005_dis.shp. list index out of range
Failed to convert: K:\FGDBIntermediate\Soil\SSURGO2_2\soilmu_a_states_dis\DE\soilmu_a_de003_dis.shp. list index out of range
Failed to convert: K:\FGDBIntermediate\Soil\SSURGO2_2\soilmu_a_states_dis\DE\soilmu_a_de001_dis.shp. list index out of range

For output feature dataset:
K:\FGDBIntermediate\Soil\SSURGO1.gdb\DE

Separator:  '_'
0 Kudos
MarilynGambone
New Contributor
Can you post input parameters and exception you get?
At the moment I would guess problem in this part:

fcSplit = outFeatureClass.split(sep)
fcName = fcSplit[2]


If sep doesn't appear in outFeatureClass there is no index [2] for fcSplit.

Regards
Arek



Hi,

I think the error is in the argument list.  When I look at the stack view when debugger a version with values in it, I can see that in "locals (Dict)" node, all the values appear.  When I run the script in ArcToolBox, it seems like it can't get the proper index to assign to the argv[] arguments.
0 Kudos
ArkadiuszMatoszka
Occasional Contributor II
I unable to get this kind of error. Everything seems to be fine. My only idea would by removing try: except: declaration and check what kind of exception is raised and from which line.
Exception is handled so it must be rise somewhere in try: section.
Cheers.
AM
0 Kudos
MarilynGambone
New Contributor
I unable to get this kind of error. Everything seems to be fine. My only idea would by removing try: except: declaration and check what kind of exception is raised and from which line.
Exception is handled so it must be rise somewhere in try: section.
Cheers.
AM


Hi,

I rewrote it using arcpy and it's working.  Here's the revised code:

#Import system modules
import arcpy
import os

#Set workspace               
inFolder = arcpy.GetParameterAsText(0)
inFtrDs = arcpy.GetParameterAsText(1)
sep = arcpy.GetParameterAsText(2)
arcpy.AddMessage("\n" + "Begin processing..." + "\n")
try:
  arcpy.env.workspace = arcpy.GetParameterAsText(0)
  fcs = arcpy.ListFeatureClasses()
  for fc in fcs: 
    fcSplit = fc.split(sep) #fc format: soilmu_a_de001_dis
    fcName = fcSplit[2]     #zero-based   
    in_Feature = inFolder + "\\" + fc
    outLocation = inFtrDs
    outFeatureClass = fcName.upper()
    expression = ""
   
    #Execute Copy
    arcpy.AddMessage("Copying shapefile:  " + fc)
    arcpy.FeatureClassToFeatureClass_conversion(in_Feature, outLocation, outFeatureClass, expression)
except:
  print arcpy.GetMessages(2)

Thanks for the input.
0 Kudos
FabianBlau
Occasional Contributor II
sys.argv[0] is the name of the script.
Your parameters starts at sys.argv[1].
So:
sys.argv[1] == arcpy.GetParameterAsText(0)
0 Kudos