2

In an arcpy for the intersect_analysis function, certain syntax works and others don't. What is the reason for this?

Works:

arcpy.Intersect_analysis("'D:\\Program Files (x86)\\ArcGIS\\data\\uscities.shp' #;'D:\\Program Files (x86)\\ArcGIS\\data\\riverbuffers2.shp' #", Cities_Near_Rivers, "ALL", "", "INPUT")
arcpy.Intersect_analysis("'D:\\Program Files (x86)\\ArcGIS\\data\\uscities.shp' #;C:\\Users\\t.thomas\\Documents\\ArcGIS\\Default.gdb\\usrivers_Buffer #", Cities_Near_Rivers, "ALL", "", "INPUT")

Does not work:

arcpy.Intersect_analysis("'D:\\Program Files (x86)\\ArcGIS\\data\\uscities.shp' #;D:\\Program Files (x86)\\ArcGIS\\data\\riverbuffers2.shp #", Cities_Near_Rivers, "ALL", "", "INPUT")

It seems that whenever a 'Program Files (x86)' is there in the path, the tool requires a single quote around the input. What is the reason for this?

This is the error I get when running this as a tool script.

Executing: testscript "D:\Program Files (x86)\ArcGIS\data\uscities.shp" "D:\Program Files (x86)\ArcGIS\data\usrivers.shp" "D:\Program Files (x86)\ArcGIS\data\testoutput23.shp"
Start Time: Mon Oct 05 13:43:07 2015
Running script testscript...
Failed script testscript...

Traceback (most recent call last):
  File "D:\Program Files (x86)\ArcGIS\data\oriscript.py", line 43, in <module>
    arcpy.Intersect_analysis("'D:\\Program Files (x86)\\ArcGIS\\data\\uscities.shp' #;D:\\Program Files (x86)\\ArcGIS\\data\\riverbuffers2.shp #", Cities_Near_Rivers, "ALL", "", "INPUT")
  File "d:\\program files (x86)\\arcgis\\desktop10.3\\arcpy\\arcpy\\analysis.py", line 289, in Intersect
    raise e
RuntimeError: Object: Error in executing tool

Failed to execute (testscript).
Failed at Mon Oct 05 13:43:12 2015 (Elapsed Time: 5.40 seconds)
2
  • 2
    I think most people would strongly urge you not to be writing data to the Program Files (x86) folder. This is traditionally where the applications are stored. Move your data to somewhere more sensible like d:\Projects\projXYZ\data.
    – Hornbydd
    Oct 5, 2015 at 9:20
  • True. But I'm just looking to know if and why parenthesis creates problems with arcpy. It could happen when working with a folder other than Program Files (x86). Thanks.
    – Thomas
    Oct 5, 2015 at 9:23

1 Answer 1

1

When there is a space in the path, it is recommended to use the

r"D:\etc" notation, so that the whole path is understood as one single string. See also the help topic Setting paths to data in Python.

4
  • That link also indicates that alternative to using r, one can use \\. But I see advantages of using an r before the string. I'm hoping to find why, not having single quotes when the path contains (x86) returns an error.
    – Thomas
    Oct 5, 2015 at 9:50
  • 2
    Spaces in paths are known to cause issues, not parenthesis. Using r before the string + single backslashes ensures your string will always be read properly. Double backslashes alone don't work in all situations.
    – GISGe
    Oct 5, 2015 at 10:17
  • This supports my suggestion of d:\Projects\projXYZ\data :)
    – Hornbydd
    Oct 5, 2015 at 10:35
  • 1
    Then there's the best practice recommendation to never use spaces or parenthesis in folder or file names. Unfortunately, it's best practice for programmers to always expect the possibility of spaces in file paths.
    – Vince
    Oct 5, 2015 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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