我正在尝试使用 delete_management 根据它们是否在列表中来删除 gdb 中的文件。我不断收到 delete_management 工具的错误,提示该文件不存在或无效。我无法弄清楚问题是什么?
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)