2

I want to perform a loop of intersections between different files in different directories. Here is my code:

Intersects = ['Name1','Name2','Name3','Name4','Name5'] #Names of my files
arcpy.env.workspace = "C:\Mydirectory" #Directory of the files above
lands =  "C:\Anotherdirectory\Shapefile_To_Intersect.shp" #Name of the second shapefile to intersect, in another directory 
#    Where I want my output to be placed, this folder is already created and empty
out = "C:\Users\Laptop\Documents\ArcGIS\Default.gdb\Intersects" 
for files in Intersects:
    Intersects_Names = ['Name1','Name2','Name3','Name4','Name5']
    arcpy.Intersect_analysis([files, lands], os.path.join(out,files))

But this way I get the error:

ERROR 000732: Input Features: Dataset Name1 #;C:\Anotherdirectory\Shapefile_to_Intersect.shp # does not exist or is not supported.

What is the issue in this case? (I checked the file name and directory are correct).

2
  • Considering that you are referencing a workspace and not a gdb in a workspace, can I assume that that the Intersects files are shapefiles? In that case you have to add .shp to each file. If you are indeed working with a gdb then you have to add that to your workspace, like: "C:\Mydirectory\yourgdb.gdb" – BritishSteel Apr 25 '16 at 14:20
  • Also, add and r before your paths to avoid issues with escape sequences. – BritishSteel Apr 25 '16 at 14:22
3

Are the feature classes in different directories? That is, your files are like:

r"C:\Mydirectory\Name1.shp"
r"C:\Mydirectory\Name2.shp"
r"C:\Anotherdirectory\Shapefile_To_Intersect.shp"

Instead of relying on workspaces, include the path with the name in your list. Then perform the intersect on that list. This intersects all of the layers together.

intersects = [r"C:\Mydirectory\Name1.shp",r"C:\Mydirectory\Name2.shp",r"C:\Mydirectory\Name3.shp",r"C:\Anotherdirectory\Shapefile_To_Intersect.shp"] 
out = "C:\Users\Laptop\Documents\ArcGIS\Default.gdb\Intersects" 
arcpy.Intersect_analysis(files,out)

Doing the join doesn't really make sense for your output. Arcpy is seeing "Intersects" in your path as a feature dataset, and trying to create a featureclass within that feature dataset.

If you want the loop to create separate intersects for each of the filenames in the list:

out = "C:\Users\Laptop\Documents\ArcGIS\Default.gdb"
for name in Intersects:
    arcpy.Intersect_analysis([name,r"C:\Anotherdirectory\Shapefile_To_Intersect.shp" ], os.path.join(out,"intesects_"+name))
0
1

Use the

arcpy.MakeFeatureLayer_management(r"C:\Anotherdirectory\Shapefile_to_Intersect.shp", "lyr_to_Intersect")
arcpy.Intersect_analysis([files, "lyr_to_Intersect"], os.path.join(out,files))

as input for your Intersect_analysis.

Intersect_analysis only accepts layers or classes not shp files.

5
  • I did as you suggested me, now the final line looks like: "arcpy.Intersect_analysis([files, arcpy.MakeFeatureLayer_management("C:\Anotherdirectory\Shapefile_to_Intersect.shp","lyr_to_Intersect")], os.path.join(out,files)) but I still receive the error message. In this case, it is exactly like before but in this case I have lyr_to_Intersect where previously there was Shapefile_to_Intersect. What may be the reason? – Patapunfate Apr 25 '16 at 13:48
  • Are you sure "C:\Anotherdirectory\Shapefile_to_Intersect.sh‌​p" is not empty? and maybe you should make the feature layer before you intersect and make sure you add the r before your inputstring of the makeFeature layer! – Kim Duyvejonck Apr 25 '16 at 13:51
  • i changed my answer like i think it should work. – Kim Duyvejonck Apr 25 '16 at 13:56
  • Unfortunately it seems this is not the reason, I still receive the same message I reported two comments above. – Patapunfate Apr 25 '16 at 14:07
  • 1
    MakeFeatureLayer shouldn't be necessary, (path to) shapefiles work fine with Intersect. That said, you should take care with all strings containing backslash characters. These should either be escaped "C:\\Temp" or prefixed with r: r"C:\Temp". Failing to do so will cause unexpected results, e.g. \t becomes a tab, \n a newline, etcetera. – Berend Apr 25 '16 at 14:39

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.