1

This is my python script code in ArcGIS 10.3;

projectResult = arcpy.Project_management(fc, outFolder + "\\" + fc, template)
#Name of shapefiles re-projected       
arcpy.AddMessage(projectResult)

Which gives me this in the geoprocessing dialog result box;

C:\GIS_Result\abc.shp
C:\GIS_Result\1234.shp
C:\GIS_Result\zyx.shp
C:\GIS_Result\987.shp

Can I remove the filepath to give me just the filename?;

abc.shp
1234.shp
zyx.shp
987.shp

  • 1
    Please note that the question you have asked here is pure Python and should probably be researched/asked at Stack Overflow rather than Geographic Information Systems. The distinction between the Python programming language and ArcPy (a Python site-package developed by Esri) will become more apparent as you work with them. – PolyGeo Apr 13 '15 at 6:21
2

It looks like you already have the shapefile names being held in your fc variable so I suggest that the simplest thing will be to just use that.

arcpy.AddMessage(fc)
  • arcpy.AddMessage(fc) only gives me 1 shapefile name in the list. – Mark W Apr 13 '15 at 23:48
  • 1
    I'm assuming that your list arose by iterating/looping something - possibly a Python list created using arcpy.ListFeatureClasses - so just place it within the same loop. – PolyGeo Apr 14 '15 at 0:32
  • PolyGeo, Great Job. I had the arcpy.AddMessage(fc) in the loop area, but entered down a line for space and comment and maybe it broke out of the loop. – Mark W Apr 14 '15 at 1:17
  • How do I leave a line space between arcpy.AddMessage(arcpy.GetMessages()) and arcpy.AddMessage(fc) in the dialog result box or is this a new question? – Mark W Apr 14 '15 at 1:24
  • The code in your question (and my answer) does not include arcpy.AddMessage(arcpy.GetMessages()) so I think you should be researching/asking a new question. Strictly speaking that would be a Python question for Stack Overflow but they will not know about ArcPy. Search there (or Google) on something like "python new line". – PolyGeo Apr 14 '15 at 1:53
1

You can use the name property of the Describe object:

arcpy.AddMessage(arcpy.Describe(projectResult).name)

>>> abc.shp

Or you could aslo use the baseName property, to print the name without the .shp extension:

arcpy.AddMessage(arcpy.Describe(projectResult).baseName)

>>> abc
0

In fact there are two (may be more) ways to do this-

  1. Using arcpy completely
  2. using arcpy and builtin library

These are illustrated below

Suppose I have three folder input, output and template

Method One

import arcpy
arcpy.env.workspace = r"C:\Users\Winrock\Desktop\gstack\Project\input"
fcs = arcpy.ListFeatureClasses()
outputFolder = r"C:\Users\Winrock\Desktop\gstack\Project\ouput"
template = r'C:\Users\Winrock\Desktop\gstack\Project\template\Template.shp'
for i in fcs:
    arcpy.AddMessage("Projecting "+ i)
    arcpy.Project_management(i,outputFolder+"\\"+i,template)

Method Two

If you want built-in library..

import arcpy,os
arcpy.env.overwriteOutput = True
inputFolder = r"C:\Users\Winrock\Desktop\gstack\Project\input"
outputFolder = r"C:\Users\Winrock\Desktop\gstack\Project\ouput"
template = r'C:\Users\Winrock\Desktop\gstack\Project\template\Template.shp'
fcs = []
for root,dirs,files in os.walk(inputFolder):
    for fc in files:
        if fc.endswith(".shp"):
            fcs.append(os.path.join(root,fc))

for i in fcs:
    print i
    name =i.split("\\")[-1] 
    print name
    arcpy.AddMessage("Projecting "+ name) //you can add newline here e.g arcpy.AddMessage("Projecting "+ '\n'+name+'\n')
    arcpy.Project_management(i,outputFolder+"\\"+name,template)

N.B. You can adapt this script to tool

  • While correct, you could give a bit more explanation. – John Powell Apr 13 '15 at 6:46
  • Take my apology, i did not get you. – SIslam Apr 13 '15 at 6:48
  • Slslam,For some reason it only gives my the name of the last shapefile. – Mark W Apr 14 '15 at 0:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.