使用python和arcpy删除地理数据库中的数据时出错

时间:2013-10-10 18:47:26

标签: python file delete-file arcpy

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

1 个答案:

答案 0 :(得分:1)

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

documentation for arcpy.da.Walk

  

产生一个包含工作空间,目录名称的三元组,   和文件名gdb

(dirpath, dirnames, and filenames)
     

注意

     

列表中的名称仅包括基本名称;没有路径组件   包括在内。获取完整路径(以top开头)到文件或   `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))