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",

# Second parameter

param1 = arcpy.Parameter(
        displayName="Nuevo campo",

    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

1 Answer 1


The reason you cannot calculate your field to contain the text hola is wrong string formatting in the expression parameter (look at the Calculate Field GP tool docs).

When using the VB parser, you need to supply a string inside double quotes. Look at the '"hola"'.

    in_table="U.S. Cities",

You could alternatively use the Python parser. You would need to reverse the strings as in "'hola'":

    in_table="U.S. Cities",

As you are writing a Python tool, it makes sense to use the Python expression instead of the VB.

  • It works. And can I specify the shapefile name instead of "hola"?
    – MikV89
    Mar 6, 2018 at 12:07
  • For sure. Look into the arcpy.Describe (resources.arcgis.com/en/help/main/10.2/index.html#/…). You would need to use the baseName property. Mar 6, 2018 at 13:35
  • And how have I use it in arcpy.CalculateField_management function? I write this: desc = arcpy.Describe(parameters[0]) and then a=arcpy.CalculateField_management(a, b, desc.baseName,"PYTHON_9.3",code_block=""). But I don't know if it is correct.
    – MikV89
    Mar 6, 2018 at 14:42
  • I get this error: ExecuteError: ERROR 000539: Error running expression: PRUEBAS Traceback (most recent call last): File "<expression>", line 1, in <module> NameError: name 'PRUEBAS' is not defined .
    – MikV89
    Mar 6, 2018 at 14:57
  • Done. x) gis.stackexchange.com/questions/273769/…
    – MikV89
    Mar 6, 2018 at 15:11

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.