Take the 2-minute tour ×

I have a mass of KML files, approx 350+ that I need in a single fGDB. I lifted a string of code from ESRI help to attempt to do this - I got it working to the point of creating all the individual gGDBs (one per KML) but its failing with an invalid character in a variable somewhere. Below is the code, and then the error that it throws out. I've added some print commands to show whats in the variables right before it fails. Help? Also, this is my first time trying to use python for actual work, so I apologize in advance for n00bish mistakes.

# Name: BatchKML_to_GDB.py
# Description: Converts a directory of KMLs and copies the output into a single fGDB.
#              A 2 step process: first convert the KML files, and then copy the featureclases

# Import system models
import arcpy, os

# Set workspace (where all the KMLs are)

# Set local variables and location for the consolidated file geodatabase
outLocation = "V:\\2155\\active\\215510456\\GDB"
MasterGDB = r'AllKMLLayers.gdb'
MasterGDBLocation = os.path.join(outLocation, MasterGDB)

# Create the master FileGeodatabase
 arcpy.CreateFileGDB_management(outLocation, MasterGDB)

# Convert all KMZ and KML files found in the current workspace
 for kmz in arcpy.ListFiles('*.KM*'):
  print "CONVERTING: " + os.path.join(arcpy.env.workspace,kmz)
  arcpy.KMLToLayer_conversion(kmz, outLocation)

# Change the workspace to fGDB location
arcpy.env.workspace = outLocation

# Loop through all the FileGeodatabases within the workspace
wks = arcpy.ListWorkspaces('*', 'FileGDB')
# Skip the Master GDB

for fgdb in wks:  

  # Change the workspace to the current FileGeodatabase
  arcpy.env.workspace = fgdb    

  # For every Featureclass inside, copy it to the Master and use the name from the original fGDB
  featureClasses = arcpy.ListFeatureClasses('*', '', 'Placemarks')
  for fc in featureClasses:
    print r"COPYING: " + fc + r" FROM: " + fgdb 
    fcCopy = fgdb + os.sep + 'Placemarks' + os.sep + fc
    print r"fcCopy = " + fcCopy
    print r"MasterGDBLocation = " + MasterGDBLocation
    print r"fgdb = " + fgdb
    arcpy.FeatureClassToFeatureClass_conversion(fcCopy, MasterGDBLocation, fgdb[fgdb.rfind(os.sep)+1:-4])

# Clean up
del kmz, wks, fc, featureClasses, fgdb

Output from shell showing error:

COPYING: Points FROM: V:\2155\active\215510456\GDB\canal-move-2-golden-gate-blvd.gdb
fcCopy = V:\2155\active\215510456\GDB\canal-move-2-golden-gate-blvd.gdb\Placemarks\Points
MasterGDBLocation = V:\2155\active\215510456\GDB\AllKMLLayers.gdb

Traceback (most recent call last):
  File "C:\Users\mperez\Desktop\batch_kml_to_gdb.py", line 46, in <module>
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\conversion.py", line 1547, in FeatureClassToFeatureClass
    raise e
ExecuteError: ERROR 000354: The name contains invalid characters
Failed to execute (FeatureClassToFeatureClass).
share|improve this question
One other this, could you add which version of ArcGIS you're using to the tags as well? (and +1 for actually posting the stacktrace - you'd be amazed at how often this doesn't happen) –  om_henners Mar 22 '13 at 22:47

2 Answers 2

First, welcome to the site. Second, welcome to Python.

In the traceback you can see where the error in the code was raised (the last line), and every function in the call leading to that error - just go backwards up the list.

From this you can see that the error was in line 46 of your code;


You know the third argument in Feature Class to Feature Class is the feature class name. Your first step should be to take a look at os.path.basename and os.path.splitext for cleaner ways to get the name.

Your second step should be to look at ValidateTableName in arcpy, which when you pass it the proposed name and workspace, will return a valid name.

Hope this helps!

share|improve this answer
What om_henners gave you is the solution, but just to understand why this particular name, canal-move-2-golden-gate-blvd failed, - is an invalid character. The list is `~@#$%^&()-+=,{}.![]; /\:*?'<>| –  blord-castillo Mar 25 '13 at 13:23

This is not meant to be an answer, just a contribution (taken from my comment on om_henners answers)

import arcpy
invalidcharacters = """`~@#$%^&()-+=,{}.![]; /\:*?'<>|"""
valid = arcpy.ValidateTableName(invalidcharacters)
print valid



(that means every character in `~@#$%^&()-+=,{}.![]; /\:*?'<>| is an invalid character)

share|improve this answer
Thanks - I wasn't sure of the complete list of invalid charactes, so I didn't want to mislead anyone! –  om_henners Mar 26 '13 at 3:48
I'm not sure that is complete either, but that's all I have been able to find :) I thought " was invalid too, but it was not for fgdb when I tested it. –  blord-castillo Mar 26 '13 at 13:11

Your Answer


