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
, doos.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