-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreate_Folders_from_FC.py
57 lines (42 loc) · 2.28 KB
/
Create_Folders_from_FC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Created by Austin Beck | austin.beck@aecom.com
# Intended to create folders/directories for every feature in a feature class/shapefile.
# The user input field's attributes will be used to name the folders/directories
# If a feature's attribute is Null or '', a folder will not be created
#------------USER INPUTS-----------------------------------------------------
input_feature_class = r''# Input feature class filepath
field_name = r'' # Field name within feature class used to generate folder name
output_folder = r'' # Output base directory for new folders
#------------DO NOT EDIT BELOW THIS LINE---------------------------------------------
print('Importing arcpy, os, time, and subprocess libraries')
import arcpy, os, subprocess, time
print('Import Complete\n')
total_time_start = time.time()
def create_folders(input_feature_class, field_name, output_folder):
# Check out the ArcGIS Spatial Analyst extension
# Make sure the output folder exists or create it
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Use a search cursor to iterate through the features
with arcpy.da.SearchCursor(input_feature_class, [field_name]) as cursor:
for row in cursor:
# Only add a folder if the field is not null
if row[0] is not None and row[0] != '':
# Get the value from the specified field
field_value = row[0]
# Create a folder name based on the field value
folder_name = str(field_value)
# Construct the full path for the new folder
folder_path = os.path.join(output_folder, folder_name)
# Create the folder if it doesn't exist
if not os.path.exists(folder_path):
os.makedirs(folder_path)
if __name__ == "__main__":
# Call the function to create folders
create_folders(input_feature_class, field_name, output_folder)
# Open the file explorer to the directory
print(f'opening {output_folder}')
explorer_cmd = f'explorer /select,"{os.path.join(output_folder,folder_name)}"'
subprocess.run(explorer_cmd, shell=True)
total_time_end = time.time() # Record the end time for the entire process
total_duration = total_time_end - total_time_start
print(f"Done!\nTotal time: {total_duration:.2f} seconds")