Select to view content in your preferred language

For nested loop is not iterating with workspace iteration

569
3
10-19-2022 08:18 PM
SadiaChowdhury
New Contributor

I will write a script that copies only the polygon shapefiles into a file geodatabase called “PPA.gdb”. It will read from the “C:/student/PPA” folder.

I must check for the existence of the PPA geodatabase whenever the script is run. If it exists, it must be deleted, and a new file geodatabase created with the same name. If it does not exist, a new file geodatabase must be created. The script must iterate through the subfolders in the PPA folder, and to iterate through the subfolders, I need to reset and list workspaces (there are two folders under PPA named TT20_3Min, TT20_5Min), and then I need to list polygon by listing feature class. 

Here is my code; my code can list the workspace but cannot list the feature class.

import arcpy as ap

# Set the current workspace
ap.env.workspace = "C:/student/PPA/"

# Confirm that the feature class exists
# If exists, it will be deleted, and a new file geodatabase created with the same name.
# If it does not exist, a new file geodatabase will be created.
# Set local variables
out_folder_path = "C:/student/PPA"
out_name = "PPA.gdb"

if ap.Exists("PPA.gdb"):
ap.management.Delete("PPA.gdb")
ap.CreateFileGDB_management("C:/student/PPA/", "PPA.gdb")
else:
ap.CreateFileGDB_management("C:/student/PPA/", "PPA.gdb")

# Listing Workspace
workspaces = ap.ListWorkspaces("*", "Folder")
print(workspaces)
# Listing datasets in workspaces
for i in workspaces:
fc = ap.ListFeatureClasses("*", "Polygon")
print(fc)

0 Kudos
3 Replies
JohannesLindner
MVP Frequent Contributor

You have to change the workspace in the loop:

import arcpy as ap

# Set the current workspace
ap.env.workspace = "C:/student/PPA/"

# Confirm that the feature class exists
# If exists, it will be deleted, and a new file geodatabase created with the same name.
# If it does not exist, a new file geodatabase will be created.
# Set local variables
out_folder_path = "C:/student/PPA"
out_name = "PPA.gdb"

# You don't need the else. Just delete the gdb if it exists, then create it
# regardless of whether it existed or not
if ap.Exists("PPA.gdb"):
    ap.management.Delete("PPA.gdb")
ap.CreateFileGDB_management("C:/student/PPA/", "PPA.gdb")

# Listing Workspace
workspaces = ap.ListWorkspaces("*", "Folder")
print(workspaces)
# Listing datasets in workspaces
for i in workspaces:
    arcpy.env.workspace = i
    fc = ap.ListFeatureClasses("*", "Polygon")
    print(fc)

 

Also, while it is customary to import some common Python modules with an alias (for example "import numpy as np"), arcpy is commonly imported without an alias. Importing it as "ap" won't result in any errors, but it might make it harder for you to exchange code with others (for example in this forum). If your course uses this alias, keep using it. If not, I suggest importing arcpy without alias.


Have a great day!
Johannes
0 Kudos
SadiaChowdhury
New Contributor

changing workspace in the loop mean? while iterating I will call folder name? my course uses this ap.

0 Kudos
JohannesLindner
MVP Frequent Contributor

ListFeatureClasses only lists feature classes in the current workspace. So to get to the shape files in the subfolders, you have to change your workspace.

workspaces = ap.ListWorkspaces("*", "Folder")
print(workspaces)
# Listing datasets in workspaces
for ws in workspaces:
    arcpy.env.workspace = ws
    fc = ap.ListFeatureClasses("*", "Polygon")
    print(fc)

 


Have a great day!
Johannes
0 Kudos