gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:46:59 25 4
gpt4 key购买 nike

我正在尝试使用delete_management根据文件是否在列表中来删除gdb中的文件。我不断收到删除管理工具的错误,提示该文件不存在或无效。我不明白问题出在哪里?

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)

最佳答案

我的猜测(我无法测试这一点)是 f 不是完整路径,因此 arcpy.Delete_management(f) 正在寻找文件工作目录(可能不是您的 gdb)。

documentation for arcpy.da.Walk

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

因此,按照该建议,我建议使用:

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

关于python - 使用 python 和 arcpy 删除地理数据库中的数据时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19303835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com