Spaces:
Sleeping
Sleeping
import os, csv | |
def get_directory_names(root_path): | |
directory_names = [] | |
# Check if the provided path exists | |
if not os.path.exists(root_path): | |
print(f"The path {root_path} does not exist.") | |
return directory_names | |
# Iterate through the items in the root path | |
for item in os.listdir(root_path): | |
item_path = os.path.join(root_path, item) | |
# Check if the item is a directory | |
if os.path.isdir(item_path): | |
directory_names.append(item) | |
return directory_names | |
def save_to_csv(directory_names, filename): | |
with open(filename, 'w', newline='') as csvfile: | |
writer = csv.writer(csvfile) | |
# writer.writerow(['Pattern Names']) # Header | |
for name in directory_names: | |
writer.writerow([name]) | |
print(f"Pattern names have been saved to {filename}") | |
# Example usage | |
root_directory = "patterns" | |
output_file = "patterns.csv" | |
directories = get_directory_names(root_directory) | |
save_to_csv(directories, output_file) | |
print("List of directories:") | |
for directory in directories: | |
print(directory) |