I am having problems trying to perform simple folder management operations in an ArcGIS python toolbox. First here's some code snippets (I have left out all of the stuff that does not pertain to this problem):
import os def getParameterInfo(self): #define the parameters for the toolbox param = arcpy.Parameter(displayName="Destination Folder", name="in_destination", datatype="DEFolder", direction="Input") params = [param] return params def execute(self, parameters, messages): #get the destination root folder parameter #I have also tried using .valueAsText but it does not solve the problem dest_root = parameters.value #if the folder does not exist create it if not os.path.exists(dest_root): os.makedirs(dest_root) #join path to store shapefiles to the root folder curr_path = os.path.join(dest_root,"/Shapefiles") #create folder to store shapefiles if not os.path.exists(curr_path): os.makedirs(curr_path) #start doing a bunch of stuff to create shapefiles
The problem I am having is that the folder parameter that ArcGIS creates is an unescaped string, so if I wanted to use the C:\Temp folder as my destination root the dest_root variable in the execute function would have a value of 'c:\temp'. This is causing problems for all of the calls to the os methods because (I think) they are escaping the characters that come after the backslash.
I have tried to replace the backslash character with a forward slash by doing this:
dest_root = dest_root.replace("\\","/")
but this does not solve the problem because the organization I'm working at uses employee ID numbers for the Windows profile user names. So if I want to use the Documents folder for a destination the escapes are being treated like hex codes:
dest_root = "C:\Users\678910\Documents" dest_root = dest_root.replace("\\","/") #dest_root is now equal to "C:/Users78910/Documents"
Have I missed something basic here? This has been driving me crazy all morning.