-1

Attempting to delete selected records in a geodatabase. The issue is this geodatabase feature classes stay the same on a weekly basis but the top level geodatabase name changes.

My questions are:

  1. How can i use python to delete selected records from the feature classes. Similar to SQL ie delete record = 'abc'?
  2. How can python detect the geodatabase change in name every time it changes without having to modify the script, so it just picks up the feature classes which will keep the same name?
import arcpy
from arcpy import env

env.workspace = "C:/data"
arcpy.CopyRows_management("accident.dbf", "C:/output/accident2.dbf")
arcpy.DeleteRows_management("C:/output/accident2.dbf")
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
LMFaz
  • 1
  • Is the gdb always found in the same file path? Do the old GDBs stay in the folder or have they been moved elsewhere in the meantime? Python in general, glob could help you determine the GDB filename. In arcpy, ListWorkspaces should work. https://gis.stackexchange.com/questions/16464/listing-feature-classes-in-multiple-geodatabases-in-folder-using-arcpy – SMiller Nov 13 '19 at 14:52
  • Hi - Is the gdb always found in the same file path? - Yes it will always be in the same place. Do the old GDBs stay in the folder or have they been moved elsewhere in the meantime? - They will stay in the same place but that step I can move it. The biggest issue is just ensuring the selected records are deleted every week when new GDB comes in with a new name. – LMFaz Nov 13 '19 at 15:00
  • Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short [tour] which emphasizes that there should be only one question asked per question. – PolyGeo Nov 14 '19 at 00:07

1 Answers1

1

I think you should actually look at the second example that your code comes from: https://pro.arcgis.com/en/pro-app/tool-reference/data-management/delete-rows.htm

That provides an example of selecting the rows from a table, then deleting the selected rows. Arcpy and ArcGIS operations tend to operate only on the selected features\rows. If nothing is selected, then it will act on the entire table\feature class.

The example you provide, copies a table then deletes all the rows.

Regarding your second question, one option is to (maybe) use the timestamp of the geodatabase to identify the most recent geodatabase folder\file inside a folder. You can probably use the os library in python for this? Unfortunately there doesn't seem to be a clear arcpy only way to get dates from a geodatabase (there is an option to use arcobjects).

dslamb
  • 2,166
  • 11
  • 10