Skip to content

Instantly share code, notes, and snippets.

@Edwardtonnn
Created December 11, 2023 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Edwardtonnn/6cb9fb15bbc114e2211d79489a02a2e8 to your computer and use it in GitHub Desktop.
Save Edwardtonnn/6cb9fb15bbc114e2211d79489a02a2e8 to your computer and use it in GitHub Desktop.
Python script to output all dir into json file
import os
import json
# Specify the directory you want to search in
directory_path = "./"
# Function to list all folders in a directory
def list_folders(directory):
folder_list = []
for root, dirs, files in os.walk(directory):
for dir_name in dirs:
folder_list.append(os.path.join(root, dir_name))
return folder_list
# Get a list of all folders in the specified directory
folders = list_folders(directory_path)
# Create a JSON representation of the folder list
json_output = json.dumps(folders, indent=4)
# Output the JSON to a file or print it
# You can change the file name as needed
with open("folder_list.json", "w") as json_file:
json_file.write(json_output)
# If you want to print the JSON to the console, you can use this instead:
# print(json_output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment