delete multiple files in python

3683
7
Jump to solution
08-13-2014 02:44 AM
ChrisGraves1
New Contributor II

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

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

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

View solution in original post

0 Kudos
7 Replies
JohannesBierer
Occasional Contributor III

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)

0 Kudos
JakeSkinner
Esri Esteemed Contributor

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

0 Kudos
JohannesBierer
Occasional Contributor III

Chris,

I think Jake offered the right solution .....

You could change '1405' in '%_1405' to get all fc with 1405 in the name

0 Kudos
ChrisGraves1
New Contributor II

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

0 Kudos
JakeSkinner
Esri Esteemed Contributor

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

0 Kudos
ChrisGraves1
New Contributor II

Thanks again Jake for your help really appreciate it. Hopefully soon I can repay you 🙂

Have a nice weekend.

Cheers,
Chris

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi Chris,

If you have a chance, be sure to mark this thread as 'Answered' to help other users in the community.  Thanks!

0 Kudos