I am using python toolbox in ArcMap, and I want to add a new field in a shapefile and after that I want to fill this field with the name of the shapefile.
I achieve to add a new field using arcpy.AddField_management function, but then I cannot fill this field. I am using arcpy.CalculateField_management function to fill it, however I think that this is a wrong way.
Any suggest is welcome.
The code is:
import arcpy, os, sys
from arcpy import env
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "Toolbox"
self.alias = ""
# List of tool classes associated with this toolbox
self.tools = [Tool]
class Tool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "FIELD"
self.description = ""
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
# First parameter
param0 = arcpy.Parameter(
displayName="Shape entrada",
name="in_features",
datatype="DEShapefile",
parameterType="Required",
direction="Input")
# Second parameter
param1 = arcpy.Parameter(
displayName="Nuevo campo",
name="field",
datatype="Field",
parameterType="Required",
direction="Input")
parameters = [param0, param1]
return parameters
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):return
def updateMessages(self, parameters):return
def execute(self, parameters, messages):
"""The source code of the tool."""
a= parameters[0].valueAsText
b= parameters[1].valueAsText
a=arcpy.AddField_management(a, b, "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
a=arcpy.CalculateField_management(a, b, "hola" ,"VB")
return a