Select to view content in your preferred language

Failed to loop through the files in a folder

1032
6
Jump to solution
07-05-2021 03:32 AM
JinseoYoon
New Contributor III

Hi, 

I am making a looping code for Intersect_analysis of multiple files in a folder, but I ended up with having only one outcome.. 

''''

### folder containing facilities
in_folder_2 = r"D:\WORK\PROJECT\COVID_19_2\GIS\CREATED_DATA\FACILITIES"

shp_file_list_2 = [ ]
path_list2 = [ ]
intersectInput_list = [ ]

for shp_file2 in os.listdir(in_folder_2):
    if shp_file2.endswith(".shp"):
        intersectInput = os.path.join(in_folder_2, shp_file2)
        intersectInput_list.append(intersectInput)

intersectInput_list  

['D:\\WORK\\PROJECT\\COVID_19_2\\GIS\\CREATED_DATA\\FACILITIES\\N3A_A0010000.shp', 'D:\\WORK\\PROJECT\\COVID_19_2\\GIS\\CREATED_DATA\\FACILITIES\\TBGIS_TRDAR_RELM_prj.shp', 'D:\\WORK\\PROJECT\\COVID_19_2\\GIS\\CREATED_DATA\\FACILITIES\\UPIS_SHP_ZON216.shp']

interFeature = r"D:\WORK\PROJECT\COVID_19_2\Created_Data\PED_FLOW\eoul_flow_time_ymd_20200401.shp"

out_folder2 = r"D:\WORK\PROJECT\COVID_19_2\GIS\CREATED_DATA\FACILITIES_INTERS"

for intersectInput in intersectInput_list:
    inFeatures = [intersectInput, interFeature]
    name = shp_file2.strip(".shp")
    clusterTolerance = 10
     intersectOutput = os.path.join(out_folder2, name)
    arcpy.Intersect_analysis(inFeatures, intersectOutput, "", clusterTolerance, "point")

 

The below is my output. Please help me, since I will increase the files in the folder much more than the current 3. 

'D:\\WORK\\PROJECT\\COVID_19_2\\GIS\\CREATED_DATA\\FACILITIES_INTERS\\UPIS_SHP_ZON216.shx'

 

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

you must surely get an error that shapefile2 is not referenced? what errors are you getting? 

intersectInput_list is also on a line without any context.

I'd also use https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/listfeatureclasses.htm when dealing with shapefiles in a folder.

I've quickly (very quickly) adjusted the code, but it is not ideal and not my finest work.

### folder containing facilities 
in_folder_2 = r"D:\WORK\PROJECT\COVID_19_2\GIS\CREATED_DATA\FACILITIES"

shp_file_list_2 = [ ]
path_list2 = [ ]
intersectInput_list = [ ]

for shp_file2 in os.listdir(in_folder_2):
    if shp_file2.endswith(".shp"):
        intersectInput = os.path.join(in_folder_2, shp_file2)
        #append a list of [name, path] for shapefile2
        intersectInput_list.append([shp_file2, intersectInput])
    
##intersectInput_list 

interFeature = r"D:\WORK\PROJECT\COVID_19_2\Created_Data\PED_FLOW\eoul_flow_time_ymd_20200401.shp"

out_folder2 = r"D:\WORK\PROJECT\COVID_19_2\GIS\CREATED_DATA\FACILITIES_INTERS"

for intersectInput in intersectInput_list:
    inFeatures = [intersectInput[1], interFeature]
    name = intersectInput[0]
    clusterTolerance = 10 
    intersectOutput = os.path.join(out_folder2, name)
    arcpy.Intersect_analysis(inFeatures, intersectOutput, "", clusterTolerance, "point")

View solution in original post

0 Kudos
6 Replies
DavidPike
MVP Frequent Contributor

I can't see where shapefile2 is referenced for the second for loop, and where outfolder2 is set, why are you stripping .shp from the filename, is the .shx extension intended?  Also the code isn't formatted so I cant see the indents, making it a mess to try and read.

JinseoYoon
New Contributor III

Thank you! 

First, the shape_file2 is referenced in the the upper part of the code (for shp_file2 in os.listdir(in_folder_2): ...)

Second, I stripped the ".shp" to use the same name of the file for the output files in a different folder. 

Third, I omitted to copy the part of the code indicating the location of the output file folder, now I added it. 

Can you please advise? 

0 Kudos
DavidPike
MVP Frequent Contributor

Even then you're outputting a file intersectOutput without an extension? That would only work if Outfolder2 was a GDB. That seems to be why only an shx file is being created.

I don't really know what's going on with ShapeFile2 unless you present your code with the proper formatting/indentation. i.e. throw us a bone.

0 Kudos
JinseoYoon
New Contributor III

Hi David, 

Thank you for your response. The problem that I have was the failing of the loop. I input the three files but only one outcome has been created. The shx. shp. extension is not an issue. I could use both types. 

I am attaching the code file, if you can advise me again - on why my loop could not go through all of the files in the list - it would be very much appreciated. 

Thank you again!! 

0 Kudos
DavidPike
MVP Frequent Contributor

you must surely get an error that shapefile2 is not referenced? what errors are you getting? 

intersectInput_list is also on a line without any context.

I'd also use https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/listfeatureclasses.htm when dealing with shapefiles in a folder.

I've quickly (very quickly) adjusted the code, but it is not ideal and not my finest work.

### folder containing facilities 
in_folder_2 = r"D:\WORK\PROJECT\COVID_19_2\GIS\CREATED_DATA\FACILITIES"

shp_file_list_2 = [ ]
path_list2 = [ ]
intersectInput_list = [ ]

for shp_file2 in os.listdir(in_folder_2):
    if shp_file2.endswith(".shp"):
        intersectInput = os.path.join(in_folder_2, shp_file2)
        #append a list of [name, path] for shapefile2
        intersectInput_list.append([shp_file2, intersectInput])
    
##intersectInput_list 

interFeature = r"D:\WORK\PROJECT\COVID_19_2\Created_Data\PED_FLOW\eoul_flow_time_ymd_20200401.shp"

out_folder2 = r"D:\WORK\PROJECT\COVID_19_2\GIS\CREATED_DATA\FACILITIES_INTERS"

for intersectInput in intersectInput_list:
    inFeatures = [intersectInput[1], interFeature]
    name = intersectInput[0]
    clusterTolerance = 10 
    intersectOutput = os.path.join(out_folder2, name)
    arcpy.Intersect_analysis(inFeatures, intersectOutput, "", clusterTolerance, "point")
0 Kudos
JinseoYoon
New Contributor III

I am sorry for the delayed response. It works well!! Thank you very much!!! Honestly I do not understand the reason clearly, but I will compare your and my code and figure out. 

Thank you very much again!! 

0 Kudos