<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic how to make long file name shorter in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-make-long-file-name-shorter/m-p/738774#M57148</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;System: ArcGIS 9.3&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Problem:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have five shapefiles with long names: &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A8923_Dis_shp.shp, A4592_Dis_shp.shp, A9820_Dis_shp.shp, A1276_Dis_shp.shp, A3726_Dis_shp.shp&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I want to make their names shorter, such as&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A8923.shp, A4592.shp, A9820.shp, A1276.shp, A3726.shp&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Please kindly advise python code to carry out the task.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 11 Jul 2012 23:28:19 GMT</pubDate>
    <dc:creator>ElaineKuo</dc:creator>
    <dc:date>2012-07-11T23:28:19Z</dc:date>
    <item>
      <title>how to make long file name shorter</title>
      <link>https://community.esri.com/t5/python-questions/how-to-make-long-file-name-shorter/m-p/738774#M57148</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;System: ArcGIS 9.3&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Problem:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have five shapefiles with long names: &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A8923_Dis_shp.shp, A4592_Dis_shp.shp, A9820_Dis_shp.shp, A1276_Dis_shp.shp, A3726_Dis_shp.shp&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I want to make their names shorter, such as&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A8923.shp, A4592.shp, A9820.shp, A1276.shp, A3726.shp&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Please kindly advise python code to carry out the task.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jul 2012 23:28:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-make-long-file-name-shorter/m-p/738774#M57148</guid>
      <dc:creator>ElaineKuo</dc:creator>
      <dc:date>2012-07-11T23:28:19Z</dc:date>
    </item>
    <item>
      <title>Re: how to make long file name shorter</title>
      <link>https://community.esri.com/t5/python-questions/how-to-make-long-file-name-shorter/m-p/738775#M57149</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;System: ArcGIS 9.3&lt;BR /&gt;I have five shapefiles with long names: &lt;BR /&gt;A8923_Dis_shp.shp, A4592_Dis_shp.shp, A9820_Dis_shp.shp, A1276_Dis_shp.shp, A3726_Dis_shp.shp&lt;BR /&gt;&lt;BR /&gt;I want to make their names shorter, such as&lt;BR /&gt;A8923.shp, A4592.shp, A9820.shp, A1276.shp, A3726.shp&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Hi Helenak,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I got this idea from &lt;/SPAN&gt;&lt;A href="http://gis.stackexchange.com/questions/27415/arcpy-rename-loop" rel="nofollow noopener noreferrer" target="_blank"&gt;gis.stackexchange&lt;/A&gt;&lt;SPAN&gt; and it works in ArcGIS 10 (I think arcpy uses the same logic as the arcgisscripting module).&amp;nbsp; If the subtext you want to change in the file name is dynamic, you might have to be more creative and use the &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;re&lt;/SPAN&gt;&lt;SPAN&gt; module.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;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:

&amp;nbsp; # get the shapefile name and file extension
&amp;nbsp; fileName,fileExtension = os.path.splitext(shapefile)
&amp;nbsp; # replace the strings you want to
&amp;nbsp; fileName = fileName.replace("_Dis_shp","")
&amp;nbsp; fileName = fileName + fileExtension
&amp;nbsp; # rename the files
&amp;nbsp; arcpy.Rename_management(shapefile, fileName)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN style="font-family:Courier New;"&gt;gis.stackexchange.com&lt;/SPAN&gt;&lt;SPAN&gt; is another great place to find the code bits you might need in the future.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I hope this helps.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:27:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-make-long-file-name-shorter/m-p/738775#M57149</guid>
      <dc:creator>K_Clark</dc:creator>
      <dc:date>2021-12-12T07:27:18Z</dc:date>
    </item>
    <item>
      <title>Re: how to make long file name shorter</title>
      <link>https://community.esri.com/t5/python-questions/how-to-make-long-file-name-shorter/m-p/738776#M57150</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Another option is "slicing":&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import os

files = filter(os.path.isfile, os.listdir(''))
for file in files:
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Split on the extension.
&amp;nbsp;&amp;nbsp;&amp;nbsp; split = file.split(".")
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Get the first 5 characters in the file name
&amp;nbsp;&amp;nbsp;&amp;nbsp; name = split[0][:5]
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Extension
&amp;nbsp;&amp;nbsp;&amp;nbsp; ext = split[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Renaming "+file+" to "+name+"."+ext
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.rename(file, name+"."+ext)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:27:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-make-long-file-name-shorter/m-p/738776#M57150</guid>
      <dc:creator>RDHarles</dc:creator>
      <dc:date>2021-12-12T07:27:21Z</dc:date>
    </item>
    <item>
      <title>Re: how to make long file name shorter</title>
      <link>https://community.esri.com/t5/python-questions/how-to-make-long-file-name-shorter/m-p/738777#M57151</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;even simpler: find the first underscore, and use everything before it. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;tack on a ".shp"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;copy to the new name&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;delete the old one (optional)&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;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:
&amp;nbsp;&amp;nbsp;&amp;nbsp; score = shapefile.find("_")
&amp;nbsp;&amp;nbsp;&amp;nbsp; short = shapefile[:score]
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Copy_management(shapefile, "%s.shp" % short)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(shapefile)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It is always good practice to use Arc for copying and changing names.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Shapefiles are pretty tolerant things... but good practice is good practice.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:27:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-make-long-file-name-shorter/m-p/738777#M57151</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2021-12-12T07:27:23Z</dc:date>
    </item>
    <item>
      <title>Re: how to make long file name shorter</title>
      <link>https://community.esri.com/t5/python-questions/how-to-make-long-file-name-shorter/m-p/738778#M57152</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the response.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Please kindly advise the function of &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1. short = shapefile[:score]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2. %s.shp&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy


for shapefile in shapefileList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; score = shapefile.find("_")
&amp;nbsp;&amp;nbsp;&amp;nbsp; short = shapefile[:score]
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Copy_management(shapefile, "%s.shp" % short)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(shapefile)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:27:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-make-long-file-name-shorter/m-p/738778#M57152</guid>
      <dc:creator>ElaineKuo</dc:creator>
      <dc:date>2021-12-12T07:27:26Z</dc:date>
    </item>
    <item>
      <title>Re: how to make long file name shorter</title>
      <link>https://community.esri.com/t5/python-questions/how-to-make-long-file-name-shorter/m-p/738779#M57153</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;&lt;BR /&gt;Please kindly advise the function of &lt;BR /&gt;1. short = shapefile[:score]&lt;BR /&gt;2. %s.shp&lt;BR /&gt;&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Helenak, I'm glad you found the answer to your original question.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To answer the questions you just posted, kindly refer to the following sources:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A class="jive-link-external-small" href="http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation" rel="nofollow" target="_blank"&gt;Slicing in Python&lt;/A&gt;&lt;BR /&gt;&lt;A class="jive-link-external-small" href="http://stackoverflow.com/questions/997797/python-and-s" rel="nofollow" target="_blank"&gt;String formatting in Python&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Google is your friend.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 13 Jul 2012 05:13:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-make-long-file-name-shorter/m-p/738779#M57153</guid>
      <dc:creator>K_Clark</dc:creator>
      <dc:date>2012-07-13T05:13:37Z</dc:date>
    </item>
  </channel>
</rss>

