4

I am trying to convert my plain python script to script in ArcToolbox to share, where user can set input data (in this case, feature class). Unfortunately, Converting Python (ArcPy) script into tool or toolbox script? did not provide an answer for me, although I tried to follow it.

This is my working script which is working good when run from python window.

import arcpy
fcLst = [r"D:\GIS_Temp\Folder B\Water.shp"]
for fc in fcLst:
fieldList = [f.name for f in arcpy.ListFields(fc) if f.type == "String"]
if fieldList:
    with arcpy.da.UpdateCursor(fc, fieldList) as cursor:
        for row in cursor:
            for i in range (len(fieldList)):
                row[i] = row[i].replace("!","1")
                cursor.updateRow(row)

I want to make script in ArcToolbox, with this code, where user can select for which feature class he wants to edit data (in this case, "D:\GIS_Temp\Folder B\Water.shp")

In ArcToolbox I created new script, clicked "store relative paths names (instead absolute paths), and browse to my python code, which I tried to edit given the answer on post I linked.

This is the python script for toolbox (which is not working):

import arcpy
fcLst = arcpy.GetParameterAsText(0)
for fc in fcLst:
fieldList = [f.name for f in arcpy.ListFields(fc) if f.type == "String"]
if fieldList:
    with arcpy.da.UpdateCursor(fc, fieldList) as cursor:
        for row in cursor:
            for i in range (len(fieldList)):
                row[i] = row[i].replace("!","1")
                cursor.updateRow(row)

As you can see, I put arcpy.GetParameterAsText(0) into code, so the user can select own data to edit. But I get this error:

Traceback (most recent call last): File "C:\Users\Name\Desktop\Script.py", line 4, in fieldList = [f.name for f in arcpy.ListFields(fc) if f.type == "String"] File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy__init__.py", line 1075, in ListFields return gp.listFields(dataset, wild_card, field_type) File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\geoprocessing_base.py", line 344, in listFields self._gp.ListFields(*gp_fixargs(args, True))) IOError: "D" does not exist Failed to execute (Script).

What am I missing? I have decent number of working codes in python, but I want to eventually convert them all to scripts in toolbox and share it with other users. What is the main process for converting the codes, and which parts of codes, given the example?

EDIT: This is the tab i edited for the script for parameters: enter image description here

  • How did you configure that parameter on the properties of your Python script tool? – PolyGeo May 27 '16 at 22:47
  • 1
    Please edit that or a picture of that tab into your question. I suggest also fixing the indentation and adding an AddMessage to report the setting of fcList that the script receives from the dialog. – PolyGeo May 27 '16 at 23:09
  • 1
    Your script has an indentation error on line 4. Python is unforgiving of incorrect indentation. Once you address that I think the AddMessage that I suggested will be much more informative about the main error. – PolyGeo May 28 '16 at 0:26
  • You are iterating through characters in your input which is not list of feature classes. Add split function by ; to get list – FelixIP May 28 '16 at 1:00
5

You are 99% there. The issue you are having is that the arcpy.getParameterAsText is returning a semi-colon separated string.(e.g. "FCpath1;FCpath2" )

This is not a list (e.g. ['FCpath1','FCpath2'] ) like you had before.

Your script is now iterating over the string, passing the first letter instead of the whole path. Based on the error we can see that your first input is from the "D" drive)

Convert your input parameter into a list like you had before using fcLst.split( ";"). That should fix it. This is because .split(";") returns a [list]

  • Where should I put it? I do not understand why I have to use split tool. Can you give an example with my code. – Dean7 May 28 '16 at 1:35
  • 1
    Replace for fc in fcList: with for fc in fcList.split(","): – Ben S Nadler May 28 '16 at 1:43
  • That worked! Just note, you wrote in your comment "fcList.split(","):", but actually it is without capital i, so your answer works great. – Dean7 May 28 '16 at 1:57
  • 2
    Sorry, phoning and walking the dog... – Ben S Nadler May 28 '16 at 1:59
  • 2
    @BenSNadler - Now that's dedication to GIS:SE :) – Joseph May 31 '16 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.