Hi Users,
I'm sure I have done this before but cannot recall how to do it. I would like to delete multiple feature classes using python but only files that have a suffix of say '1405' or '1305' for example. The main file name would be different e.g. 'NorthernWells_1405' & SouthernWells_1405' so use the suffix in the code to find these files and remove them.
In addition to this I would also like to rename files with a suffix like '1406' but again have a different file name.
Has anyone written some python code like this?
Any assistance would be much appreciated,
Chris
Solved! Go to Solution.
Chris,
You will just need to use the Rename function to do this. Ex:
for fc in arcpy.ListFeatureClasses("*"):
if <some criteria> in fc:
arcpy.Rename_management(fc, fc + "_1406")
import arcpy
from arcpy import env
import os
# Set the workspace for the ListFeatureClass function
#
env.workspace = "c:/Temp1"
# Use the ListFeatureClasses function to return a list of
# shapefiles.
#
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
print fc
if fc == "herkunft.shp":
arcpy.Delete_management(fc)
Hi Chris,
The below code will delete feature classes from the 'test.gdb' that contain '1405' or '1305'. It will also rename any feature class that contains '1406' to text before the underscore. For example, 'SouthernWells_1406' would be renamed to 'SouthernWells'.
import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"
for fc in arcpy.ListFeatureClasses("*"):
if '1405' in fc or '1305' in fc:
arcpy.Delete_management(fc)
elif '1406' in fc:
arcpy.Rename_management(fc, fc.split("_")[0])
Chris,
I think Jake offered the right solution .....
You could change '1405' in '%_1405' to get all fc with 1405 in the name
Hi Guys,
Thankyou very much for your help with this. I have got it working thankyou. I would also like to rename files and give them a suffix of _1406 for example. The existing file names are all different so they would need to stay the same.
Thanks again very much appreciated.
Chris
Chris,
You will just need to use the Rename function to do this. Ex:
for fc in arcpy.ListFeatureClasses("*"):
if <some criteria> in fc:
arcpy.Rename_management(fc, fc + "_1406")
Thanks again Jake for your help really appreciate it. Hopefully soon I can repay you 🙂
Have a nice weekend.
Cheers,
Chris
Hi Chris,
If you have a chance, be sure to mark this thread as 'Answered' to help other users in the community. Thanks!