This week’s lab focused on creating a Python script that copies shapefiles into a geodatabase, then uses a Search Cursor to pull city data and build a dictionary of County Seat populations. It sounds like a lot but breaking it into steps made it easier to follow and troubleshoot along the way.
The first thing I did was organize my project folder. I added subfolders for Data, Scripts, Results, and Project. Everything I created or edited for the lab stayed in those folders so I could keep track of my files.
I started the script by setting the workspace and using arcpy.CreateFileGDB_management() to make a new geodatabase called Mod5_MG.gdb. I used overwriteOutput = True so it wouldn’t break if I needed to rerun it. Then I listed all shapefiles in the data folder with arcpy.ListFeatureClasses() and copied them into the new geodatabase using a for loop.
To avoid shapefile extension errors, I used arcpy.Describe().baseName so the output names didn’t include .shp. This fixed the issue where it couldn’t write to the geodatabase if I left the extension in.
Once everything was copied, I set up a Search Cursor on the cities feature class and filtered it to only include cities marked as “County Seat.” I printed the name, feature type, and population for each one, then used those values to build a dictionary with city names as keys and populations as values. 


Problems I Ran Into:
At first, the cursor didn’t return any cities because I hadn’t updated the workspace after copying the files. Once I pointed the workspace to the new geodatabase, it worked. I also tried printing the population directly at first and forgot to make sure it was formatted correctly when displayed with text, but adding f-string formatting cleared that up.
This lab helped me see how much easier it is to manage large amounts of data using loops and cursors, especially when you print as you go to catch errors early. Running the full script at the end gave me all the output in one place, which made it easy to grab screenshots and confirm it all worked.


Comments
Post a Comment