Select to view content in your preferred language

Create a table in code with arcpy.CreateTable_management()

8889
3
07-10-2011 07:11 AM
AndreLong
Emerging Contributor
Hello my name is Andre and I need to create a table in code with arcpy.CreateTable_management() but I keep getting this error below:

ARCPY ERRORS:
ERROR 000354: The name contains invalid characters
Failed to execute (CreateTable).


PYTHON ERRORS:
Traceback Info:
  File "F:\Geog375\assignmentsWeeks2to3\longAndreAssignment2.py", line 158, in <module>
    arcpy.CreateTable_management(geoDataBasePath , 'sacramentoTable.dbf')

Error Info:
     <class 'Queue.Empty'>:


Here is my code:

# Created by: Andre Long
# Created on: June 25, 2011

import arcpy, sys, traceback, datetime

author = 'A. Long'

# reformat date to MM.DD.YYYY
# NOTE: lowercase %y will result in MM.DD.YY format
# See Python.org or text regarding the datetime module

CUR_DATE = datetime.date.today().strftime('%B, %x')


# Paths will likely be different on a local system
# Change these as necessary

datapath =   'F:\\Geog375\\assignmentsWeeks2to3\\SacramentoData\\'
outputpath = 'F:\\Geog375\\assignmentsWeeks2to3\\output\\'
geoDataBase_out_folder_path = outputpath + 'geoDataBase\\'
geoDataBasePath = outputpath + 'geoDataBase\\AndreGeoDataBase.gdb'
geoDataBaseName = 'AndreGeoDataBase.gdb'

neighborhoods_shp = datapath + 'Sacramento_Neighborhoods.shp' #'Sacramento_Neighborhoods.shp'
streets_shp = datapath + 'Sacramento_Streets.shp'
city_facilities_shp = datapath + 'City_Facilities.shp'

# Add variables for feature layers and table here

neighborhood_class = 'Sacramento_Neighborhoods.shp'  # for example
neighborhood_layer = 'neighborhood'

streets_class = 'Sacramento_Streets.shp'
streets_layer = 'streets'

cityFacilities_class = 'City_Facilities.shp'
cityFacilities_layer = 'cityFacilities'

andreSacTable = geoDataBasePath + 'SacramentoTable.dbf'

print str("Starting Session 2 Script... \n")

print str("Current Date is: " + CUR_DATE + "\n")

try:

    ### Check to see if feature classes exist (note: these are not feature layers)
    # Make sure to use the correct

    # For each feature class: neigbhorhoods, streets, and city facilities
    # check to see if the output feature class (the feature class in
    # the file geodtabase exists.  If it exists, delete it.
    # Use the method describe in the Exercise and respective Python Primer chapters

    # Neighborhoods Feature Class

    # For example for neighborhoods
    #if arcpy.Exists(outputpath + 'neighborhoods'):
    #   arcpy.Delete_management(outputpath + 'neighborhoods')

   
   
    if arcpy.Exists(outputpath + 'neighborhoods'):
       arcpy.Delete_management(outputpath + 'neighborhoods')
       print str("Deleted old feature class " + neighborhood_class + " \n")
    else:
       print str("File does not exist \n")

    if arcpy.Exists(outputpath + 'streets'):
       arcpy.Delete_management(outputpath + 'streets')
       print str("Deleted old feature class " + streets_class + " \n")
    else:
       print str("File does not exist \n")

    if arcpy.Exists(outputpath + 'facilities'):
       arcpy.Delete_management(outputpath + 'facilities')
       print str("Deleted old feature class " + cityFacilities_class + " \n")
    else:
       print str("File does not exist \n")
   

    ### Convert(Import) shapefiles to file geodatabase feature classes

    # Next use the FeatureClassToFeatureClass routine to "import" the
    # shapefile into a file geodatabase feature class


    arcpy.FeatureClassToFeatureClass_conversion(neighborhoods_shp , outputpath, 'neighborhoods')

    # Streets Feature Class
    arcpy.FeatureClassToFeatureClass_conversion(streets_shp, outputpath, 'streets')

    # City Facilities Feature Class
    arcpy.FeatureClassToFeatureClass_conversion(city_facilities_shp, outputpath, 'facilitiesCity')
   
    print str("All three implementations of FeatureClassToFeatureClass_conversion have been completed \n ")
   
    ### Check to see if feature layers exist

    # Check to see if the feature layers exist; if they do, delete them
    # in the same manner as above.  With the feature layers, only
    # the feature layer name is required to check for the existance
    # and delete routines.

    # Next use the MakeFeatureLayer routine  to create feature layers
    # for each feature class

    # Neighborhoods Feature Layer
    if arcpy.Exists(neighborhood_layer):
        arcpy.Delete_management(neighborhood_layer)
        arcpy.MakeFeatureLayer_management(neighborhoods_shp, neighborhood_layer)
        print str("Deleted old feature layer" + neighborhood_layer + "and created new feature layer \n")
    else:
        arcpy.MakeFeatureLayer_management(neighborhoods_shp, neighborhood_layer)
        print str("Created a new feature layer" + neighborhood_layer + "\n")

    # Streets Feature Layer
    if arcpy.Exists(streets_layer):
        arcpy.Delete_management(streets_layer)
        arcpy.MakeFeatureLayer_management(streets_shp, streets_layer)
        print str("Deleted old feature layer" + streets_layer + "and created new feature layer \n")
    else:
        arcpy.MakeFeatureLayer_management(streets_shp, streets_layer)
        print str("Created a new feature layer" + streets_layer + "\n")


    # City Facilities Feature Layer
    if arcpy.Exists(cityFacilities_layer):
        arcpy.Delete_management(cityFacilities_layer)
        arcpy.MakeFeatureLayer_management(city_facilities_shp, cityFacilities_layer)
        print str("Deleted old feature layer" + cityFacilities_layer + "and created new feature layer \n")
    else:
        arcpy.MakeFeatureLayer_management(city_facilities_shp, cityFacilities_layer)
        print str("Created a new feature layer" + cityFacilities_layer + "\n")

   
    ### Create a new table in the file geodatabase
   
    # Use the CreateTable routine to do this
    # Use the neighborhood stats table variable you create above
    # Make sure to use the Exists and Delete statements appropriately
    # to check for the existence of the table.

    # first create the database which ArcGis calls a geodatabase
    if arcpy.Exists(geoDataBasePath):
        arcpy.Delete_management(geoDataBasePath)
        arcpy.CreateFileGDB_management(geoDataBase_out_folder_path, geoDataBaseName)
        print str("Old database deleted and new database created \n")
    else:
        arcpy.CreateFileGDB_management(geoDataBase_out_folder_path, geoDataBaseName)
        print str("New Geo Data Base created \n")

    if arcpy.Exists(andreSacTable):
        arcpy.Delete_management(andreSacTable)
        arcpy.CreateTable_management(outputpath, andreSacTable)
        print str("Old table deleted and created new table \n")
    else:
        arcpy.CreateTable_management(geoDataBasePath , 'sacramentoTable.dbf') #this line is failing
        print str("New table created \n")


    ### Add Fields using the AddField routine
   
    # Since we are not using a workspace to access the table in
    # the file geodatabase you will need to use both the outputpath
    # and the neighborhood stats table variables in this routine


    ### Create a query statement and assign to a variable

    # The query will use the Name field and be set to 'Downtown' using
    # proper use of escape characters
    # This assignment will only query a single neigbohood (Downtown).

    ### Select the specific neighborhood using the query (SelectLayerbyAttribute)

    # Select the downtown neighborhood using the query as a parameter
    # in the SelectLayerByAttribute routine.  This will be a "new"
    # selection.

    ### Count the number of selected elements

    # Use the getCount routine to count the number of "selected" features
    # Create a print statemtnt to write this number to the Python Shell
    # make sure to "cast" the result to a string using str(<count variable name>)
    # See exercise document for example syntax.

    ### Select all streets within the selected neighborhood (SelectLayerByLocation)

    # Select all of the streets within the selected Neighborhood
    # Use the SelectLayerByLocation routine to select all of the streets
    # that "Intersect" the selected neighborhood.

    # This will be a new selection and will use "Intersect" as the type
    # of relationship

    ### Count the number of selected streets


    ### Copy the selected features to a new feature class

    # Use CopyFeatures to copy the selected features to a new feature class
    # in the file geodatabase.
    # Use the name 'Downtown' as part of the feature class name.
    # Make sure to check for the existence of this feature class using the
    # methods above.
   
    ### Select all city facilities within the selected neighborhood (SelectLayerByLocation)

    # Follow the same procedure as the streets

    # Get a count of the selected features and write this to the Python Shell

    # Copy the selecte city facilities to an output feature class in the file
    # geodatabase.  Use the name 'Downtown' as part of the feature class name.
    # Make sure to check for the existence of this feature class.
   
    print 'Session 2 Script Complete.'

except:

   
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n     " +        str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"
    msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"

    arcpy.AddError(msgs)
    arcpy.AddError(pymsg)

    print msgs
    print pymsg
   
    arcpy.AddMessage(arcpy.GetMessages(1))
    print arcpy.GetMessages(1)

Any help will be appreciated thank you in advanced.
Tags (2)
0 Kudos
3 Replies
StephanieWendel
Esri Contributor
It looks you are trying to store a dbf table inside of the file geodatabase. The current output location is set to your geodatabase.

geoDataBasePath = outputpath + 'geoDataBase\\AndreGeoDataBase.gdb'

arcpy.CreateTable_management(geoDataBasePath , 'sacramentoTable.dbf') #this line is failing

You cannot name the table with .dbf in the title within the geodatabase. I tested this in the python window with a sample geodatabase that I had and received the same error when I gave it "sacramentoTable.dbf" as the output name.

So if you want it to go in your geodatabase, simply remove the .dbf from the name. If it should actually be a dbf table, change the output location variable to the correct path that is not storing it within a geodatabase. Give that a try and see if it works. There is no need to specify a format for the table if you are saving it into a geodatabase.
0 Kudos
AndreLong
Emerging Contributor
I will try your advice today and see how it works
0 Kudos
AndreLong
Emerging Contributor
Yes that did it I can now create a table Thank You  Thank You  Thank You
0 Kudos