如何删除文件基本名称的一部分并将其附加到文件名的末尾?

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

我在一个文件中有多个特征类,我需要根据指定的文件重新投影它们。我正在编写一个 Arcpy 脚本(将用于在 Arcmap 中创建一个工具)来执行此操作。

如何删除文件名的开头并将其添加到结尾?您可以看到,为了 运行 arcpy.Project_management 工具,我必须指定 output_feature_class 前面有一个文本字符串,以便我可以正确使用该工具。例如,我需要它说 shapeFile_projected.shp 而不是 "projected_shapeFile.shp"。

我为此编写了一个 "for" 循环,所以我想对所有重新投影的特征 类 执行此操作。

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

我还想在打印消息时去掉名称中的“.shp”,可以吗?

我不知道哪个语句给出了文件名。 用给出输出的语句替换输入

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'

循环内部:

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