Sunday, 17 November 2019

arcpy - Python workspace


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")

Answer



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"


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...