Skip to content

Instantly share code, notes, and snippets.

@AspireOne
Created March 31, 2024 14:37
Show Gist options
  • Save AspireOne/10da9a284371cf111a71726fb565b971 to your computer and use it in GitHub Desktop.
Save AspireOne/10da9a284371cf111a71726fb565b971 to your computer and use it in GitHub Desktop.
A Python script that gets the folder/file structure of a given web project. Common folders (node_modules, .git) are ignored.
import os
def list_files(startpath, output_file, list_files=True):
ignored_directories = {"node_modules", "out", "ios", "public", "locales", "uploads", "volumes", "prisma", ".husky", ".turbo", "android", ".next", ".github", ".git", ".idea", ".vscode", ".vscode-counter"}
with open(output_file, 'w') as f_out:
for root, dirs, files in os.walk(startpath, topdown=True):
# Update dirs in-place to skip ignored directories
dirs[:] = [d for d in dirs if all(not root.replace(startpath, "").startswith(ignored) and d != ignored for ignored in ignored_directories)]
# Skip files at the root directory
if root == startpath:
continue
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 4 * (level - 1)
f_out.write(f"{indent}- {os.path.basename(root)}/\n")
if list_files:
subindent = ' ' * 4 * (level)
for f in files:
f_out.write(f"{subindent}* {f}\n")
if __name__ == "__main__":
directory_path = input("Enter the directory path: ")
output_file = input("Enter the output file path: ")
user_choice = input("Do you want to list only directories (D) or both directories and files (B)? (D/B): ").strip().upper()
if user_choice not in ['D', 'B']:
print("Invalid choice. Please enter 'D' for directories only or 'B' for both directories and files.")
else:
list_files_option = True if user_choice == 'B' else False
if os.path.exists(directory_path):
list_files(directory_path, output_file, list_files_option)
print(f"Directory structure written to {output_file}")
else:
print("The specified directory does not exist.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment