Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

This script runs fine the first time but fails when run the second time. The issue seems to be that the KMLToLayer_conversion statement creates a file geodatabase (no surprise) that subsequently cannot be deleted even when the feature class is removed from the map, the layer file is deleted, and the geodatabase contents are deleted. I'd like clean up after myself when this script is done with all traces gone other then the new feature classes within MasterGDB. The problem is that this script can only be run once unless you exit ArcMap, manually delete the folder in Windows, then re-start ArcMap. Running the individual commands within the Python window inevitably shows a "" but the directory for the geodatabase remains. So what's this newbie missing here? (In this test/debug script, there is only a single KML file "C:\Temp\KKKLLL.KML" -- which converts just fine).

import arcpy, os
# Name: BatchKML_to_GDB.py
# Source: AS16818.ZIP from acripts.esri.com


import arcpy, os

# Set local variables and location for the consolidated file geodatabase
KMLDir = "C:\TEMP\KML3"
outLocation = "C:\\Temp\\MuleDeer"
MasterGDB = 'AllKLM5.gdb'
MasterGDBLocation = os.path.join(outLocation, MasterGDB)

# Create the master FileGeodatabase as needed
if not (arcpy.Exists(MasterGDBLocation)):
    print MasterGDBLocation + " doesn't exist; creating it now"
    arcpy.CreateFileGDB_management(outLocation, MasterGDB)

# Convert all KMZ and KML files found in the current workspace    
# Set workspace (where all the KMLs are)
arcpy.env.workspace=KMLDir
for kmz in arcpy.ListFiles('*.KM*'):

  print "CONVERTING: " + os.path.join(arcpy.env.workspace,kmz)
  kmz2 = os.path.join(arcpy.env.workspace,kmz)
  arcpy.KMLToLayer_conversion(kmz2, outLocation)
  print "Done"

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

# Loop through all the FileGeodatabases within the workspace
wks = arcpy.ListWorkspaces('*', 'FileGDB')
# Drop Master GDB from the array/list
wks.remove(MasterGDBLocation)

for fgdb in wks:  
  # Change the workspace to the current FileGeodatabase
  arcpy.env.workspace = fgdb    

  featureClasses = arcpy.ListFeatureClasses('*', '', 'Palacemarks')
  for fc in featureClasses:
    fcCopy = fgdb + os.sep + 'Placemarks' + os.sep + fc    
    arcpy.FeatureClassToFeatureClass_conversion(fcCopy, MasterGDBLocation, fgdb[fgdb.rfind(os.sep)+1:-4])

arcpy.Delete_management(fcCopy)
arcpy.Delete_management("C:\\Temp\\Muledeer\\KKKLLL.lyr")
arcpy.Delete_management(fgdb)
share|improve this question
    
try add arcpy.env.overwriteOutput = True and del wks, kmz,... maybe it help –  user7172 Jul 11 '12 at 9:30
    
Use RefreshCatalog before deleting the folder help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//… –  iRfAn Jul 11 '12 at 13:21
    
Thanks for both your comments. The "overwriteOutput" in particular is a gem. The RefreshCatalog didn't seem to have any impact on the issue. On the other hand the original code worked fine this morning, with the new overwriteOutput line. I suspect the system reboot was part of the magic (sigh). Now to turn it into a production version "del" statement included. –  Tyla Jul 11 '12 at 17:54

2 Answers 2

Your identation level needs to be indented for:

arcpy.Delete_management(fgdb)

fgdb is a item within your loop that its reference cannot be accessed how you have it now.

Try:

for fgdb in wks:  
  # Change the workspace to the current FileGeodatabase
  arcpy.env.workspace = fgdb    

  featureClasses = arcpy.ListFeatureClasses('*', '', 'Palacemarks')
  for fc in featureClasses:
    fcCopy = fgdb + os.sep + 'Placemarks' + os.sep + fc    
    arcpy.FeatureClassToFeatureClass_conversion(fcCopy, MasterGDBLocation, fgdb[fgdb.rfind(os.sep)+1:-4])

  arcpy.Delete_management(fgdb)

arcpy.Delete_management(fcCopy)
arcpy.Delete_management("C:\\Temp\\Muledeer\\KKKLLL.lyr")
share|improve this answer

I've found that the following delete call completes correctly..

if arcpy.Exists(fcName):
   arcpy.Delete_management(fcName)
share|improve this answer
1  
I'm not sure how this answers the OP's question. –  Devdatta Tengshe Oct 22 '13 at 4:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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