0

I wrote a simple script to rename about 15 shapefiles in a folder on our local server. When I tested it I was working on a workspace on the C:\ and it worked fine. All our data is on a local server and when I changed the pathname to (... arcpy.env.workspace = "P:\Alabama\Export") it did not work. I eventually have to use 20 or so diff paths so I don't want to have to copy data to my c drive.

As I read through examples they all seem to use something on C:.

I am running it in the Python window in ArcCatalog not on an IDE

Sample:

import arcpy

arcpy.env.workspace = "P:\Alabama\Export"

 if arcpy.Exists("Histori2.shp"):
    arcpy.Rename_management("Histori2.shp", "H_BEV_CANS.shp")

 if arcpy.Exists("Histori3.shp"):
     arcpy.Rename_management("Histori3.shp", "H_CANS.shp")
  • C: is just the standard drive letter, so that is why it is used for examples. – Paul Aug 8 '13 at 22:05
10

In Python, the backslash character is treated as an escape character. \n is the newline character, \t is a tab....

There are a few different ways you can get it to work:

  1. Change your backslashes to forward slashes (Python accepts either). "P:/Alabama/Export"
  2. Double your backslashes (escape the escape). "P:\\Alabama\\Export"
  3. Format as raw string (escape characters are ignored). r"P:\Alabama\Export"
  • 5
    I'm a fan of 3, since it's easy to do r'' and copy/paste a raw path from the File Explorer path. Super quick and easy. – Mike T Aug 8 '13 at 22:13
  • 6
    +1 @Mike Toews. And in case anyone didn't know the "super trick" for copying the full path and filename of a file from Windows Explorer, SHIFT + (Right-click) on a file provides a normally "hidden" extra option to "Copy as Path". – RyanDalton Aug 8 '13 at 23:03
  • 1
    @RyanDalton that's a really strange option to hide in a extra menu. – Nathan W Aug 8 '13 at 23:41
  • 2
    WHAAAAT?! Gosh, and here I was right clicking and navigating through the tabs, highlighting the object name. Why in the world isn't this in the normal right click? – Paul Aug 8 '13 at 23:42
  • You can get a shell extension called Copy Path that adds the item directly to the context menu and works on XP as well as 7. Such a huge timesaver. – blah238 Aug 9 '13 at 2:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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