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
    What is the error you get with the code in your question? Remove fc =, fc will be a result object which you dont seem to use. If user does not set env.workspace = outpath, add field and insertcursor will not find newFC. Either set workspace to outpath or use os.path.join(outpath, newFC) in addfield and cursor – BERA Aug 18 '19 at 19:05
  • Please update your original question post (using the 'EDIT' button) to include your error message. If you've resolved the question, please post the resolution as an answer. – Son of a Beach Aug 18 '19 at 22:36

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, privacy policy and cookie policy

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