pythonappendarcpyarcmap

How do I remove part of a file base name and attach it to the end of the file name?


I have multiple feature classes in a file, and I need to reproject them based on a specified file. I am writing an Arcpy script (will be used to create a tool in Arcmap) to do this.

How do I remove the beginning of the file name and add it onto the end? You can see that in order to run the arcpy.Project_management tool, I had to designate the output_feature_class to have a text string in front of it so that I could use the tool properly. For instance, instead of saying "projected_shapeFile.shp", I need it to say shapeFile_projected.shp.

I have a "for" loop written for this, so I would like to do it for all the feature classes that are reprojected.

#Import modules
import arcpy, os

#Set workspace directory
from arcpy import env

#Define workspace
inWorkspace = arcpy.GetParameterAsText(0)
env.workspace = inWorkspace
env.overwriteOutput = True

#Define local feature class to reproject to
targetFeature = arcpy.GetParameterAsText(1)

#Describe the input feature class 
inFc = arcpy.Describe(targetFeature)
sRef = inFc.spatialReference

#Describe input feature class
fcList = arcpy.ListFeatureClasses()

#Loop to re-define the feature classes
for fc in fcList:
    desc = arcpy.Describe(fc)
    if desc.spatialReference.name != sRef.name:
        print "Projection of " + str(fc) + " is " + desc.spatialReference.name + ", so re-defining projection now:\n"
        newFc = arcpy.Project_management(fc, "projected_" + fc, sRef)
        arcpy.AddMessage(arcpy.GetMessages())
        newFc = arcpy.Describe(newFc)
        count = arcpy.GetMessageCount()
        print "The reprojection of " + str(newFc.baseName) + " " + arcpy.GetMessage(count-1) + "\n"

I would also like to drop the ".shp" from the name when I print the message, is that possible?


Solution

  • I don't know which statement gives the filename. Replace the input with the statement that gives the output

    name = "projected_shapeFile.shp" #here add the statement that outputs the filename 
    name = name[:name.find('.')] #skip this if you want to keep ".shp" extension
    name = name.split('_')
    name = name[1] +'_' +name[0]
    name
    
    'shapeFile_projected'
    

    Inside the loop:

    #Loop to re-define the feature classes
    for fc in fcList:
        desc = arcpy.Describe(fc)
        if desc.spatialReference.name != sRef.name:
            print "Projection of " + str(fc) + " is " + desc.spatialReference.name + ", so re-defining projection now:\n"
            newFc = arcpy.Project_management(fc, "projected_" + fc, sRef)
            newFc = newFc[:name.find('.')] #skip this if you want to keep ".shp" extension
            newFc = name.split('_')
            newFc = newFc[1] +'_' +newFc[0]
            arcpy.AddMessage(arcpy.GetMessages())
            newFc = arcpy.Describe(newFc)
            count = arcpy.GetMessageCount()
            print "The reprojection of " + str(newFc.baseName) + " " + arcpy.GetMessage(count-1) + "\n"