Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am working in a work space in python on ArcGIS 10.1 on a remote desktop. Here is my code

import arcpy
from arcpy import env
env.workspace = "E:\Example"

PST = arcpy.ListFeatureClasses ("", "Point")
LST = arcpy.ListFeatureClasses ("", "Polyline")

for point in PST:
    for polyline in LST:
        arcpy.SplitLineAtPoint_management (polyline, point, point + "500" + ".shp", 1)

I want the output feature class to have the same file name as the point feature class, but with 500 appended to the end. Currently the way the code is now, it will name the output "Example.shp500.shp" which is giving me an error of failing to execute. How can I name my output "Example500.shp"?

share|improve this question
1  
NOTE: If you are using windows pathnames with one slash (\), you need to convert your path to a raw string literal (where you set the env.workspace variable) using the r directive (see the section at the bottom of the page listed under Paths and the escape character) –  RyanDalton Apr 25 at 14:50

2 Answers 2

up vote 3 down vote accepted
PST = arcpy.ListFeatureClasses ("", "Point")
LST = arcpy.ListFeatureClasses ("", "Polyline")

for point in PST:
    for polyline in LST:
        arcpy.SplitLineAtPoint_management (polyline, point, point[:-4] + "500" + ".shp", 1)

[:-4] means all but the last four characters.

but you will have a problem with your name if you keep your code like this, because there will be one point[:-4] + "500" + ".shp" for each line. you should use something like :

polyline[:-4] + "_" + point[:-4] + "500" + ".shp"
share|improve this answer

A more Pythonic way which does not require guessing the file extension's length would be to use the built-in os.path module. You can use the basename method to get just the filename portion of the path, then return the first entity (filename) returned by the splitext (read carefully, that is split-ext, not split-text) method.

Additionally, you should not have to include the ".shp" component of your output filename. The shapefile is the default output and ArcGIS will automatically append the extension when writing to a folder.

import arcpy, os
from arcpy import env
env.workspace = r"E:\Example"

PST = arcpy.ListFeatureClasses ("", "Point")
LST = arcpy.ListFeatureClasses ("", "Polyline")

for point in PST:
    for polyline in LST:
        fname = os.path.splitext(os.path.basename(point))[0] + "500"
        arcpy.SplitLineAtPoint_management (polyline, point, fname, 1)
share|improve this answer
    
you are right, but the OP is clearly using a shp, and most of the time you will have 4 characters anyway. Furthermore, you should know the output type in order to choose the extension, or you need additional testng for that (by the way, you need to add 500.shp in the case of the OP). –  radouxju Apr 25 at 18:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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