Select to view content in your preferred language

how to make long file name shorter

4642
5
Jump to solution
07-11-2012 04:28 PM
ElaineKuo
Regular Contributor
System: ArcGIS 9.3

Problem:

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

Please kindly advise python code to carry out the task.
Thank you
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
K_Clark
Occasional Contributor


Please kindly advise the function of
1. short = shapefile[:score]
2. %s.shp



Helenak, I'm glad you found the answer to your original question.

To answer the questions you just posted, kindly refer to the following sources:

Slicing in Python
String formatting in Python

Google is your friend.

View solution in original post

0 Kudos
5 Replies
K_Clark
Occasional Contributor
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


Hi Helenak,

I got this idea from gis.stackexchange and it works in ArcGIS 10 (I think arcpy uses the same logic as the arcgisscripting module).  If the subtext you want to change in the file name is dynamic, you might have to be more creative and use the re module.

Make sure all your shapefiles are in one folder and that an instance of the ArcMap document referring to those shapefiles (or ArcCatalog) isn't open.

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)


gis.stackexchange.com is another great place to find the code bits you might need in the future.

I hope this helps.
0 Kudos
RDHarles
Regular Contributor
Another option is "slicing":

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)
0 Kudos
markdenil
Frequent Contributor
even simpler: find the first underscore, and use everything before it.
tack on a ".shp"
copy to the new name
delete the old one (optional)
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)


It is always good practice to use Arc for copying and changing names.
Shapefiles are pretty tolerant things... but good practice is good practice.
0 Kudos
ElaineKuo
Regular Contributor
Thanks for the response.

Please kindly advise the function of
1. short = shapefile[:score]
2. %s.shp


import arcpy


for shapefile in shapefileList:
    score = shapefile.find("_")
    short = shapefile[:score]
    arcpy.Copy_management(shapefile, "%s.shp" % short)
    arcpy.Delete_management(shapefile)
0 Kudos
K_Clark
Occasional Contributor


Please kindly advise the function of
1. short = shapefile[:score]
2. %s.shp



Helenak, I'm glad you found the answer to your original question.

To answer the questions you just posted, kindly refer to the following sources:

Slicing in Python
String formatting in Python

Google is your friend.
0 Kudos