Solved! Go to Solution.
Please kindly advise the function of
1. short = shapefile[:score]
2. %s.shp
System: ArcGIS 9.3
I have five shapefiles with long names:
A8923_Dis_shp.shp, A4592_Dis_shp.shp, A9820_Dis_shp.shp, A1276_Dis_shp.shp, A3726_Dis_shp.shp
I want to make their names shorter, such as
A8923.shp, A4592.shp, A9820.shp, A1276.shp, A3726.shp
import os import arcpy from arcpy import env # directory containing shapefiles to be renamed shapefile_dir = path = r'C:\ChangeShapefileNames\Shapefiles' env.workspace = shapefile_dir shapefileList = arcpy.ListFeatureClasses() for shapefile in shapefileList: # get the shapefile name and file extension fileName,fileExtension = os.path.splitext(shapefile) # replace the strings you want to fileName = fileName.replace("_Dis_shp","") fileName = fileName + fileExtension # rename the files arcpy.Rename_management(shapefile, fileName)
import os files = filter(os.path.isfile, os.listdir('')) for file in files: # Split on the extension. split = file.split(".") # Get the first 5 characters in the file name name = split[0][:5] # Extension ext = split[1] print "Renaming "+file+" to "+name+"."+ext os.rename(file, name+"."+ext)
import arcpy # directory containing shapefiles to be renamed shapefile_dir = path = r'C:\ChangeShapefileNames\Shapefiles' arcpy.env.workspace = shapefile_dir shapefileList = arcpy.ListFeatureClasses() for shapefile in shapefileList: score = shapefile.find("_") short = shapefile[:score] arcpy.Copy_management(shapefile, "%s.shp" % short) arcpy.Delete_management(shapefile)
import arcpy for shapefile in shapefileList: score = shapefile.find("_") short = shapefile[:score] arcpy.Copy_management(shapefile, "%s.shp" % short) arcpy.Delete_management(shapefile)
Please kindly advise the function of
1. short = shapefile[:score]
2. %s.shp