1

我正在尝试使用 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)
4

1 回答 1

1

我的猜测(我无法测试这个)是f不是完整路径,所以arcpy.Delete_management(f)在工作目录中寻找文件(可能不是你的gdb)。

文档arcpy.da.Walk说_

产生一个三元组,包括工作区、目录名和文件名(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`.

注意

列表中的名称仅包括基本名称;不包括路径组件。要获取 中的文件或目录的完整路径(以顶部开头)dirpath,请执行os.path.join(dirpath, name).

所以按照这个建议,我会说使用:

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

找到此类错误的一个好方法是只打印您正在寻找的文件名:

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
于 2013-10-10T19:13:08.530 回答