문제

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)
도움이 되었습니까?

해결책

My guess (I can't test this) is that f isn't the full path, so arcpy.Delete_management(f) is seeking the file in the working directory (which is probably not your gdb).

The documentation for arcpy.da.Walk says

Yields a tuple of three that includes the workspace, directory names, and file names (dirpath, dirnames, and filenames).

`dirpath` is the path to the workspace as a string.
`dirnames` is a list of names of subdirectories and other workspaces in `dirpath`.
`filenames` is a list of names of non-workspace contents in `dirpath`.

Note:

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).

So following that advice, I'd say use:

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

A good way to find this type of bug is to just print the filenames that you're looking for:

for gdb, fd, fc in arcpy.da.Walk(out_loc):
    for f in fc:
        print "checking whether to keep", f
        if not any(f in fl_nm for fl_nm in fl_nm_lst):
            to_del = os.path.join(gdb,f)
            print "deleting", to_del
            arcpy.Delete_management(to_del)
        print "keeping", f
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top