I have a dilemma, I am trying to create a new feature class, but I want the user to be able to input the name of the new fc.

I have created the variable 'newFC' as getparameterastext(2), but how do I enter the variable into the CreateFeatureclass_management line (as the out_name)?

If I enter it as below I get an error, if I enter it as "newFC" i get a feature class named newFC.

import arcpy
from arcpy import env
import csv
from arcpy import da

env.overwriteOutput = True

env.workspace = arcpy.GetParameterAsText(0)       
inputText = arcpy.GetParameterAsText(1)
outpath = arcpy.GetParameterAsText(0)
newFC = arcpy.GetParameterAsText(2)

textfile = open (inputText, "r+")

fc = arcpy.CreateFeatureclass_management(outpath, newFC, "POLYGON")
arcpy.AddField_management(newFC, "Parcel", "TEXT")       
cursor = arcpy.da.InsertCursor(newFC, ["SHAPE@","Parcel"])
array = arcpy.Array()
point = arcpy.Point()

parcel = ""

for line in textfile:              
    splitline = line.split(" ")     
    if splitline[0] == "Spatial":   
        sr = int(splitline[2])      
        spatialreference = arcpy.SpatialReference(sr)   
        print "Spatial reference is", spatialreference.name  
    elif splitline[0] == "Name:":           
        parcel = splitline[1]                
    elif splitline[0] == "\n":              
        polygon = arcpy.Polygon(array)      
        array = arcpy.Array()              
        ex = splitline[0]       
        point.X = ex[:-1]      
        point.Y = splitline[1]  

del cursor          

arcpy.DefineProjection_management(fc, spatialreference)

mxd = arcpy.mapping.MapDocument("CURRENT")
df=arcpy.mapping.ListDataFrames(mxd, "*")[0]
layer_name = newFC

arcpy.MakeFeatureLayer_management(fc, layer_name)
add_layer = arcpy.mapping.Layer(layer_name)
arcpy.mapping.AddLayer(df, add_layer)

print "Success!"

edit: I am adding getparameterastext(2) as a string and the error message I get is:

Traceback (most recent call last): File "\SNHU-LABS-UTIL2.snhu-labs.com\userprofiles$\william.herzig\Documents\programming\module_7_data\module_7_data\week7final\week7ba.py", line 17, in fc = arcpy.CreateFeatureclass_management(outpath, newFC, "POLYGON") File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\management.py", line 2019, in CreateFeatureclass raise e ExecuteError: ERROR 000354: The name contains invalid characters Failed to execute (CreateFeatureclass).


2 Answers 2


From the comments it's clear that the issue is bad characters in your output name. I recommend using ValidateTableName to ensure your inputs won't throw the exception.

newFc = arcpy.ValidateTableName (newFc, outPath)
arcpy.CreateFeatureclass_management(outpath, newFC, "POLYGON")

just got it to work, I had a space in the string...replace with underscore and it worked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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