<?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 Removing characters from a Feature Class file name in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026711#M59959</link>
    <description>&lt;P&gt;This should be a simple answer ... I'm trying to just remove the first 25 characters from a list (&amp;gt;90 in number) of Feature classes (raster datasets) within a .gdb.&lt;/P&gt;&lt;P&gt;All the file names are named the same except for the last 9 digits (hence why I am trying to remove the first 25).&lt;BR /&gt;I would then like to add some standard text to all the FCs (in the example "Hello")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My attempt from other threads comprises the following (but has not worked so far):&lt;/P&gt;&lt;P&gt;-------------------&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;import os&lt;/P&gt;&lt;P&gt;dir = r'C:\Users\X\Desktop\X\X.gdb'&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;arcpy.env.workspace = dir&lt;/P&gt;&lt;P&gt;for ras in arcpy.ListRasters ("*"):&lt;BR /&gt;print (ras)&amp;nbsp; &amp;nbsp; #original raster name&lt;BR /&gt;basename = ras.split("*")[:25]&amp;nbsp; &amp;nbsp; #strips the first 25 characters from original raster name&lt;BR /&gt;newrastername = "Hello" + basename&lt;BR /&gt;print (newrastername)&lt;BR /&gt;arcpy.CopyRaster_management(ras, newrastername)&amp;nbsp; &amp;nbsp;#create a new raster with the new/updated name&lt;/P&gt;&lt;P&gt;----------------------&lt;/P&gt;&lt;P&gt;Error received:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class="ansi-red-fg"&gt;---------------------------------------------------------------------------&lt;/SPAN&gt;
&lt;SPAN class="ansi-red-fg"&gt;SyntaxError&lt;/SPAN&gt;                               Traceback (most recent call last)
File &lt;SPAN class="ansi-blue-fg"&gt;C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\ast.py&lt;/SPAN&gt;, in &lt;SPAN class="ansi-green-fg"&gt;parse&lt;/SPAN&gt;:
Line &lt;SPAN class="ansi-blue-fg"&gt;35&lt;/SPAN&gt;:    &lt;SPAN class="ansi-blue-fg"&gt;return&lt;/SPAN&gt; &lt;SPAN class="ansi-cyan-fg"&gt;compile&lt;/SPAN&gt;(source, filename, mode, PyCF_ONLY_AST)

&lt;SPAN class="ansi-red-fg"&gt;SyntaxError&lt;/SPAN&gt;: Missing parentheses in call to 'print'. Did you mean print(ras #original raster name)? (&amp;lt;string&amp;gt;, line &lt;span class="lia-unicode-emoji" title=":smiling_face_with_sunglasses:"&gt;😎&lt;/span&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 14 Feb 2021 21:35:33 GMT</pubDate>
    <dc:creator>MichaelTomiak</dc:creator>
    <dc:date>2021-02-14T21:35:33Z</dc:date>
    <item>
      <title>Removing characters from a Feature Class file name</title>
      <link>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026711#M59959</link>
      <description>&lt;P&gt;This should be a simple answer ... I'm trying to just remove the first 25 characters from a list (&amp;gt;90 in number) of Feature classes (raster datasets) within a .gdb.&lt;/P&gt;&lt;P&gt;All the file names are named the same except for the last 9 digits (hence why I am trying to remove the first 25).&lt;BR /&gt;I would then like to add some standard text to all the FCs (in the example "Hello")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My attempt from other threads comprises the following (but has not worked so far):&lt;/P&gt;&lt;P&gt;-------------------&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;import os&lt;/P&gt;&lt;P&gt;dir = r'C:\Users\X\Desktop\X\X.gdb'&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;arcpy.env.workspace = dir&lt;/P&gt;&lt;P&gt;for ras in arcpy.ListRasters ("*"):&lt;BR /&gt;print (ras)&amp;nbsp; &amp;nbsp; #original raster name&lt;BR /&gt;basename = ras.split("*")[:25]&amp;nbsp; &amp;nbsp; #strips the first 25 characters from original raster name&lt;BR /&gt;newrastername = "Hello" + basename&lt;BR /&gt;print (newrastername)&lt;BR /&gt;arcpy.CopyRaster_management(ras, newrastername)&amp;nbsp; &amp;nbsp;#create a new raster with the new/updated name&lt;/P&gt;&lt;P&gt;----------------------&lt;/P&gt;&lt;P&gt;Error received:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class="ansi-red-fg"&gt;---------------------------------------------------------------------------&lt;/SPAN&gt;
&lt;SPAN class="ansi-red-fg"&gt;SyntaxError&lt;/SPAN&gt;                               Traceback (most recent call last)
File &lt;SPAN class="ansi-blue-fg"&gt;C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\ast.py&lt;/SPAN&gt;, in &lt;SPAN class="ansi-green-fg"&gt;parse&lt;/SPAN&gt;:
Line &lt;SPAN class="ansi-blue-fg"&gt;35&lt;/SPAN&gt;:    &lt;SPAN class="ansi-blue-fg"&gt;return&lt;/SPAN&gt; &lt;SPAN class="ansi-cyan-fg"&gt;compile&lt;/SPAN&gt;(source, filename, mode, PyCF_ONLY_AST)

&lt;SPAN class="ansi-red-fg"&gt;SyntaxError&lt;/SPAN&gt;: Missing parentheses in call to 'print'. Did you mean print(ras #original raster name)? (&amp;lt;string&amp;gt;, line &lt;span class="lia-unicode-emoji" title=":smiling_face_with_sunglasses:"&gt;😎&lt;/span&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Feb 2021 21:35:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026711#M59959</guid>
      <dc:creator>MichaelTomiak</dc:creator>
      <dc:date>2021-02-14T21:35:33Z</dc:date>
    </item>
    <item>
      <title>Re: Removing characters from a Feature Class file name</title>
      <link>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026712#M59960</link>
      <description>&lt;P&gt;Can you share your print statement returns and your error messages?&amp;nbsp; It looks ok, but what is&amp;nbsp;&lt;SPAN&gt;ras.split("*") for?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Feb 2021 21:31:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026712#M59960</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-02-14T21:31:07Z</dc:date>
    </item>
    <item>
      <title>Re: Removing characters from a Feature Class file name</title>
      <link>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026713#M59961</link>
      <description>&lt;P&gt;yes apologies&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is to try to remove the first 25 (took the split from some research on line, but likely not the most sensible function to use)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Feb 2021 21:36:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026713#M59961</guid>
      <dc:creator>MichaelTomiak</dc:creator>
      <dc:date>2021-02-14T21:36:53Z</dc:date>
    </item>
    <item>
      <title>Re: Removing characters from a Feature Class file name</title>
      <link>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026714#M59962</link>
      <description>&lt;P&gt;split produces pieces, you have to determine which one first then slice off the characters.&amp;nbsp; For example&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;ras = "left*right"

ras.split("*")[0][:2]
'le'

ras.split("*")[1][:2]
'ri'

ras.split("*")[0][-2:]
'ft'

ras.split("*")[1][-2:]
'ht'&lt;/LI-CODE&gt;&lt;P&gt;or skip the split&lt;/P&gt;&lt;LI-CODE lang="python"&gt;ras[-5:]
'right'

ras[:5]
'left*'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Feb 2021 21:54:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026714#M59962</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-02-14T21:54:59Z</dc:date>
    </item>
    <item>
      <title>Re: Removing characters from a Feature Class file name</title>
      <link>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026715#M59963</link>
      <description>&lt;P&gt;well that's not quite right.&amp;nbsp; Just use&amp;nbsp;&lt;/P&gt;&lt;P&gt;basename = ras[:25]&lt;/P&gt;&lt;P&gt;and forget about the .split("*") unless you want to create a list of strings split at an asterisk *.&amp;nbsp; in that case use&lt;/P&gt;&lt;P&gt;basename = ras.split("*")[0][:25]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit - I also think you need [25:] not [:25]&lt;/P&gt;</description>
      <pubDate>Sun, 14 Feb 2021 22:03:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026715#M59963</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-02-14T22:03:17Z</dc:date>
    </item>
    <item>
      <title>Re: Removing characters from a Feature Class file name</title>
      <link>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026720#M59964</link>
      <description>&lt;P&gt;thank you. However I'm still getting an 'indentation' error:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class="ansi-red-fg"&gt;IndentationError&lt;/SPAN&gt;                          Traceback (most recent call last)
File &lt;SPAN class="ansi-blue-fg"&gt;C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\ast.py&lt;/SPAN&gt;, in &lt;SPAN class="ansi-green-fg"&gt;parse&lt;/SPAN&gt;:
Line &lt;SPAN class="ansi-blue-fg"&gt;35&lt;/SPAN&gt;:    &lt;SPAN class="ansi-blue-fg"&gt;return&lt;/SPAN&gt; &lt;SPAN class="ansi-cyan-fg"&gt;compile&lt;/SPAN&gt;(source, filename, mode, PyCF_ONLY_AST)

&lt;SPAN class="ansi-red-fg"&gt;IndentationError&lt;/SPAN&gt;: expected an indented block (&amp;lt;string&amp;gt;, line 10)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Original code:&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;import os&lt;/P&gt;&lt;P&gt;dir = r'C:\Users\Michael.Tomiak\Desktop\25\Copy.gdb'&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;arcpy.env.workspace = dir&lt;/P&gt;&lt;P&gt;for ras in arcpy.ListRasters ("*"):&lt;BR /&gt;print (ras)&lt;BR /&gt;basename = ras[25:] #strips the first 25 characters from original raster name&lt;BR /&gt;newrastername = basename&lt;BR /&gt;print (newrastername)&lt;BR /&gt;arcpy.CopyRaster_management(ras, newrastername) #create a new raster with the new/updated name&lt;/P&gt;</description>
      <pubDate>Sun, 14 Feb 2021 22:37:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026720#M59964</guid>
      <dc:creator>MichaelTomiak</dc:creator>
      <dc:date>2021-02-14T22:37:24Z</dc:date>
    </item>
    <item>
      <title>Re: Removing characters from a Feature Class file name</title>
      <link>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026721#M59965</link>
      <description>&lt;LI-CODE lang="markup"&gt;import arcpy
import os

dir = r'C:\Users\X\Desktop\X\X.gdb'


arcpy.env.workspace = dir

for ras in arcpy.ListRasters ("*"):
    #print (ras)    #original raster name
    #print(ras.split("*"))
    basename = ras[25:]    #strips the first 25 characters from original raster name
    newrastername = "Hello" + basename
    print (newrastername)
    arcpy.CopyRaster_management(ras, newrastername)   #create a new raster with the new/updated name&lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 14 Feb 2021 22:44:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026721#M59965</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-02-14T22:44:22Z</dc:date>
    </item>
    <item>
      <title>Re: Removing characters from a Feature Class file name</title>
      <link>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026722#M59966</link>
      <description>&lt;P&gt;Code formatting on the esri Community will help&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_blank"&gt;Code formatting ... the Community Version - GeoNet, The Esri Community&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Feb 2021 22:58:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026722#M59966</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-02-14T22:58:37Z</dc:date>
    </item>
    <item>
      <title>Re: Removing characters from a Feature Class file name</title>
      <link>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026725#M59967</link>
      <description>&lt;P&gt;Thanks. Worked well. Didn't know indentation was required for it to run. Thought it was a presentation aspect.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;WRT to the last line of code, what function could replace this so that it was only renaming the original files, rather than making a copy of the data (which is useful sometimes) but I do not have much data storage capacity left. Making a copy might be tight.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;Would it be something like:&lt;BR /&gt;arcpy.RenameRaster_management (ras, newrastername)&lt;/P&gt;</description>
      <pubDate>Sun, 14 Feb 2021 23:11:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026725#M59967</guid>
      <dc:creator>MichaelTomiak</dc:creator>
      <dc:date>2021-02-14T23:11:14Z</dc:date>
    </item>
    <item>
      <title>Re: Removing characters from a Feature Class file name</title>
      <link>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026727#M59968</link>
      <description>&lt;P&gt;Simple oneliner for fun, IF the 25 characters are all the same.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;newrastername = ras.replace('THISIS25CHARACTERSLONGMAN', 'Hello')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Feb 2021 23:15:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026727#M59968</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-02-14T23:15:31Z</dc:date>
    </item>
    <item>
      <title>Re: Removing characters from a Feature Class file name</title>
      <link>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026728#M59969</link>
      <description>&lt;P&gt;Cheers Dan. This is really helpful as I am definitely not a coder and learning/applying only the very basics of Python to help me with the GIS aspect of my technical work.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used Rename tool for this with %Name% in the output name field originally, but that's only helpful for appending information and not (as my original Question suggestions) that helpful for removing, or slicing up filenames.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Feb 2021 23:16:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026728#M59969</guid>
      <dc:creator>MichaelTomiak</dc:creator>
      <dc:date>2021-02-14T23:16:00Z</dc:date>
    </item>
    <item>
      <title>Re: Removing characters from a Feature Class file name</title>
      <link>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026733#M59970</link>
      <description>&lt;LI-CODE lang="python"&gt;import arcpy
#import os #youre not using os so dont import it

dir = r'C:\Users\X\Desktop\X\X.gdb'


arcpy.env.workspace = dir

for ras in arcpy.ListRasters ("*"):
    #print (ras)    #original raster name
    #print(ras.split("*"))
    basename = ras[25:]    #strips the first 25 characters from original raster name
    newrastername = "Hello" + basename
    print (newrastername)
    arcpy.Rename_management(ras, newrastername)   #rename raster with the new/updated name&lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 14 Feb 2021 23:32:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026733#M59970</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-02-14T23:32:39Z</dc:date>
    </item>
    <item>
      <title>Re: Removing characters from a Feature Class file name</title>
      <link>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026739#M59971</link>
      <description>&lt;P&gt;Worked a treat &lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;- thanks for your time and effort David.&amp;nbsp; Saved me an awful lot of manual renaming tonight.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2021 00:49:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-characters-from-a-feature-class-file-name/m-p/1026739#M59971</guid>
      <dc:creator>MichaelTomiak</dc:creator>
      <dc:date>2021-02-15T00:49:17Z</dc:date>
    </item>
  </channel>
</rss>

