I am trying to join two lists of feature class files from separate folders into a new folder. I've been successful spatially joining a single file but cannot figure out how to list files from folder 1, files form folder 2 - and then join them together. Here is what I have tried so far:
# Import Packages
import arcpy, os, sys, string
from arcpy import env
from arcpy.sa import *
#Call the Spatial license
arcpy.CheckOutExtension("Spatial")
#Define the outfolder
outFolder = r"C:\OutFolder"
#Set working environment and list files from folder one
env.workspace = r"C:\Folder1"
#List Files from Folder 1
featureclasses = arcpy.ListFeatureClasses()
for inFeature1 in featureclasses:
print inFeature1
#Set working environment and list files from folder two
env.workspace = r"C:\Folder2"
#List Files from Folder 2
featureclasses = arcpy.ListFeatureClasses()
for inFeature2 in featureclasses:
print inFeature2
#Create file names for the joined file
outFeature = outFolder + "/" + inFeature2
arcpy.SpatialJoin_analysis(inFeature1, inFeature2, outFeature, "JOIN_ONE_TO_ONE", "KEEP_ALL", "", "INTERSECT", "", "")
print "done"
Here is the error I am getting:
Target Features: Dataset 20180201.shp does not exist or is not supported Failed to execute (SpatialJoin).
I think the error has something to so with the filenames possible being the same between the two folders. At the very least, I know the ordering of my lists and the application of the function are incorrect.
Any suggestions?
New error after code update:
Target Features: Dataset \wrrs1\Student Homes\Group Projects\TRMM_GPM_Showdown\DiffFiles\Joined Final20180201円.shp does not exist or is not supported Failed to execute (SpatialJoin).
1 Answer 1
20180201.shp
is a data set from the folder r"C:\Folder1"
. After finding this shapefile you change the workspace to r"C:\Folder2"
. Thus when you reference the variable inFeature1
in your spatial join python can no longer find the shapefile.
To solve this problem I would use the full path for the shapefile path string in variable inFeature1
.
Something like this:
import os
"""Other code here"""
#Set working environment and list files from folder one
env.workspace = r"C:\Folder1"
#List Files from Folder 1
featureclasses = arcpy.ListFeatureClasses()
for inFeature1 in featureclasses:
inFeature1 = os.path.join (r"C:\Folder1", inFeature1)
print inFeature1
"""continue with the rest of the code"""
Now python will check the full path and thus the correct folder for inFeature1
.
-
Thanks - I added the code for inFeature one and am getting the same error. I will edit the above post for your reference. Thanks.Rachel Rotz– Rachel Rotz2018年07月12日 13:14:43 +00:00Commented Jul 12, 2018 at 13:14
-
@ Emil. Actually, I made a mistake. I am re-running now and will get back. Thanks for your help.Rachel Rotz– Rachel Rotz2018年07月12日 13:26:43 +00:00Commented Jul 12, 2018 at 13:26
Explore related questions
See similar questions with these tags.