Created
December 11, 2023 22:45
-
-
Save Edwardtonnn/6cb9fb15bbc114e2211d79489a02a2e8 to your computer and use it in GitHub Desktop.
Python script to output all dir into json file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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