2

I'm using a simple script to run Integrate tool in a default gdb and feature class, where user can indicate the path where this gdb is located.

This script works well when path indicated by user don't have any white spaces, but gives an error when path contain spaces.

I know that I need to use path in a raw string format (r' '). But, how to do this since path is a parameter inputed by user?

import arcpy
import os
from os import path
from arcpy import env
from arcpy import da
from arcpy import management

# user input - GDB path
gdb_dir = arcpy.GetParameterAsText(0)

gdb = "Analysis.gdb"
fdsND = "ND"
network = "roads"
fullnetwork = os.path.join(gdb_dir,gdb,fdsND,network)

desc = arcpy.Describe(fullnetwork)
SR = desc.spatialReference

arcpy.Integrate_management (fullnetwork, "1.0 Meters")

Using tool with path containing spaces

Error

  • 1
    Your input is "D:\Doc\Temp\Tool test\folder", try this instead "D:\Doc\Temp\Tool test\xxxfolder" because \f is treated as a form feed. Just something to try? – Hornbydd Sep 12 '17 at 17:58
  • I just tested it, but didn't work. The problem really looks like the space in "Tool test" folder, because when I change the folder name to "Tooltest", removing the space, it works. I don't consider estabilsh rules in folder names, because I want to distribute this tool and ideally it should work with any path. It's strange, because I have used a lot of other arcpy functions using folders with spaces and works fine. Just the Integrate tool gives this error. – avrechi Sep 12 '17 at 18:27
2

Yes, there is a problem with spaces in layer's path when running Integrate (and some other tools).

To avoid this try to set the environment variable env.workspace to layer's base directory (or database). Then run the tool using only the name of your feature class without full path.

  • Thanks @Serge Norin ! Now it's working! Sometimes I use arcpy tools with layers or outputs in different directories, therefore I used to use fullpath. – avrechi Sep 13 '17 at 13:40
2

It looks like you're getting the parameter from the user, then defining them further in a hard-coded script. You could put all the parameters in the user's hands:

import arcpy
from arcpy import env

env.workspace = arcpy.GetParameterAsText(0)
gdb = arcpy.GetParameterAsText(1)
in_features = arcpy.GetParameterAsText(2)
integrate_features = arcpy.GetParameterAsText(3)

Then set the parameter data types in the script properties.

Selecting parameter data types

Assuming you aren't already setting the parameter data types-- if you are setting them, and you're receiving path errors due to spaces, it might be an underlying constraint popping up when ArcPy passes the function into ArcObjects.

  • I already set the data type. Since gdb structure has a pattern (gdb name, folders and features, because it's a output of previous script), it's unnecessary (and not useful) to put all parameters in user's hands. I tested inputing the feature class directly (instead of folder), and I have the error due to space in folder name. It really might be a underlying constraint. I was wondering if there's some way to convert the input parameter in a raw string in the script. When I use the path directly in the script, like this: r'D:\Documentos\TEMP\Tool test\folder\Analysis.gdb\ND\roads', it works. – avrechi Sep 12 '17 at 19:23
  • 1
    From what I recall, most (if not all) arcpy functions are just feeding parameters into their ArcObjects equivalent. Some functions handle spaces just fine, some just break on the gui tool dialogue (and running them in the Python console works fine), but some just don't work. Are you feeding it in as env.workspace? EDIT: I've noticed the answer above, apologies. – aubj Sep 13 '17 at 19:47
0

I tried to create a geodatabase in a folder that has spaces in the name, C:\temp\test\spaces in it\abc.gdb\ND\roads

I created a Toolbox.tbx in the 'spaces in it' folder and a script tool. The script is also in that folder. When I run it, I don't get an error message and the describe function works fine. I am not trying to run the Integrate_management path, but I added a line arcpy.AddMessage(SR.name) and it prints the correct name of the spatial reference.

Are you setting the parameter name to Data Type = 'Folder'? Make sure in the General tab, the Name does not have spaces in it, the Label can have spaces.

Script properties

  • Hi Mattropolis, I had set the parameter as "Folder" and there's no space in name in General tab. The problem is exactly with the Integrate function (I tried print the message of SR and works fine also). It looks like some Arcpy functions doesn't suport spaces in file and path name (got it from: geonet.esri.com/thread/51953). – avrechi Sep 12 '17 at 17:43

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.