2

I am trying to use delete_management to delete files in a gdb based on whether or not they are in a list. I keep getting an error with the delete_management tool that says the file either does not exist or is not valid. I can't figure out what the problem is?

import arcpy, os, easygui, sys
mxd_path = easygui.enterbox("Enter directory where mxds are located:",title='Search for Data Sources')
lyr_lst = []
lyr_lst_files = []
fl_nm_lst = []
FCList = []
RList =[]
#out_loc = easygui.enterbox("Enter ouput directory for copying files:")
out_loc = r'C:\GIS\Assimilate_Mxd_Data\Assimilate_Mxd_Data_TESTING\Output Data\test_output.gdb'
#set mxd and output geodatabase directory paths, exit if not specified
if mxd_path == None or mxd_path == '':
 sys.exit()
if out_loc == None or out_loc == '':
 sys.exit()
#generate a list of feature classes and a list of rasters already exist in
#output geodatabase
for gdb, fd, fc in arcpy.da.Walk(out_loc,datatype='FeatureClass'):
 for f in fc:
 FCList.append(f)
for gdb, fd, rasters in arcpy.da.Walk(out_loc,datatype='RasterDataset'):
 for rstr in rasters:
 RList.append(rstr)
#walk through mxds in mxd path and generate unique list of layers sourced in all
#mxd documents
for dirpath, dirnames, filenames in os.walk(mxd_path):
 for filename in filenames:
 fullPath = os.path.join(dirpath, filename)
 basename, extension = os.path.splitext(filename)
 if extension == ".mxd":
 mxd = arcpy.mapping.MapDocument(fullPath)
 LyrList = arcpy.mapping.ListLayers(mxd)
 for item in LyrList:
 if item.supports("DATASOURCE"):
 lyr_lst.append(item.dataSource)
lyr_lst_unique = list(set(lyr_lst))
#retrieve file names without extension from unique mxd layers lists, to compare
#with contents of output gdb
for lyr in lyr_lst_unique:
 path, file = os.path.split(lyr)
 fl_nm = os.path.splitext(file)[0]
 fl_nm_lst.append(fl_nm)
#compare file names in lyr_list_unique to lists of feature classes and rasters that
#already exist in output gdb. If file names are not in FCList and RList, meaning
#they do not already exist in the gdb, copy files to output gfb
 if fl_nm not in FCList and fl_nm not in RList:
 try:
 arcpy.FeatureClassToGeodatabase_conversion(lyr,out_loc)
 except:
 print 'Input file ' + lyr + ' is not a feature class'
 try:
 arcpy.RasterToGeodatabase_conversion(lyr,out_loc)
 except:
 print 'Input file ' + lyr + ' is not a feature class or a raster'
#compare file names in fl_nm_list to lists of feature classes and rasters that
#already exist in output gdb. If files exist in gdb that are not found in mxd
#unique layers list, delete
 for gdb, fd, fc in arcpy.da.Walk(out_loc):
 for f in fc:
 if f not in fl_nm_lst:
 arcpy.Delete_management(f)
asked Oct 10, 2013 at 18:35
7
  • 1
    Have you tried using os.path.join() on f and printing the value? You can also use arcpy.Exists() to see if the arcpy will recognize the path as existing or not. Commented Oct 10, 2013 at 18:44
  • I did print the f values and they were as I expected.. I dont think the delete tool takes a path as input, but a layer Commented Oct 10, 2013 at 18:49
  • 1
    Not an answer to your question, but you may find it easier to use easygui.diropenbox to return the name of a directory and eliminate user mis-spellings. Commented Oct 10, 2013 at 18:51
  • If you look at the online help for Delete_management, it shows it does take "data element" as an option. What do you get if you print out "f" right before your last line? Might the featureclass be in a Feature Dataset? Commented Oct 10, 2013 at 18:54
  • i guess i am getting a string list element Commented Oct 10, 2013 at 18:57

1 Answer 1

1

f variable only contains the name of the featureclass.

You could set the environment workspace variable

arcpy.env.workspace = out_loc # "C:/data/base.gdb"

Or you could join the path as the ESRI help suggest:

Names in the lists include only the base name; no path components are included. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).

In your case replace the last line with:

arcpy.Delete_management(os.path.join(gdb, fc))

Please note that I'm sure about witch is better if you have featureclass in dataset.

answered Oct 18, 2013 at 19:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.