ALERT!
Click here to register with a few steps and explore all our cool stuff we have to offer!

Jump to content



Photo

Folder Trees Generator - a Simple Python Script to Generate Folder Trees in TXT file.


  • Please log in to reply
Folder Trees Generator - a Simple Python Script to Generate Folder Trees in TXT file.

#1

friedx
friedx
    Offline
    0
    Rep
    1
    Likes

    New Member

Posts: 14
Threads: 7
Joined: Jan 10, 2023
Credits: 0
One year registered
#1

Hi everyone! 

I’ve built a tool called Folder Tree Generator by Friedx , which lets you easily generate and export folder structures as text files. It’s perfect for organizing, documenting, or analyzing directory hierarchies. 

Features:

- Generate a folder tree for any directory. 

- Preview the folder structure in the app. 

- Export the structure as a `.txt` file. 

 

Full Script:
 

import os
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
def generate_folder_tree(path, depth=None, exclude=None):
    exclude = exclude or []
    tree_lines = []
    def walk_dir(dir_path, prefix="", level=0):
        if depth is not None and level > depth:
            return
        try:
            entries = sorted(os.listdir(dir_path))
        except PermissionError:
            return
        for i, entry in enumerate(entries):
            if entry in exclude:
                continue
            full_path = os.path.join(dir_path, entry)
            connector = "\u251c\u2500\u2500 " if i < len(entries) - 1 else "\u2514\u2500\u2500 "
            tree_lines.append(f"{prefix}{connector}{entry}")
            if os.path.isdir(full_path):
                extension = "\u2502   " if i < len(entries) - 1 else "    "
                walk_dir(full_path, prefix + extension, level + 1)
    tree_lines.append(path)
    walk_dir(path)
    return "\n".join(tree_lines)
def save_tree_to_file(tree, folder_name, save_location):
    file_path = os.path.join(save_location, f"{folder_name}.txt")
    with open(file_path, "w", encoding="utf-8") as file:
        file.write(tree)
def browse_folder():
    folder_path = filedialog.askdirectory(title="Select Folder")
    if folder_path:
        folder_path_var.set(folder_path)
        preview_folder_tree()
def choose_save_location():
    save_location = filedialog.askdirectory(title="Select Save Location")
    if save_location:
        save_location_var.set(save_location)
def preview_folder_tree():
    folder_path = folder_path_var.get()
    if folder_path:
        try:
            tree = generate_folder_tree(folder_path)
            preview_text.delete(1.0, tk.END)
            preview_text.insert(tk.END, tree)
        except Exception as e:
            messagebox.showerror("Error", f"An error occurred: {e}")
def export_tree():
    folder_path = folder_path_var.get()
    save_location = save_location_var.get()
    if not folder_path:
        messagebox.showwarning("Warning", "Please select a folder.")
        return
    if not save_location:
        messagebox.showwarning("Warning", "Please select a save location.")
        return
    try:
        folder_name = os.path.basename(folder_path)
        tree = generate_folder_tree(folder_path)
        save_tree_to_file(tree, folder_name, save_location)
        messagebox.showinfo("Success", f"Folder tree exported to {save_location}")
    except Exception as e:
        messagebox.showerror("Error", f"An error occurred: {e}")
# Main application window
root = tk.Tk()
root.title("Folder Tree Generator by Friedx")
# Variables
folder_path_var = tk.StringVar()
save_location_var = tk.StringVar()
# Interface elements
frame = ttk.Frame(root, padding="10")
frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
folder_label = ttk.Label(frame, text="Folder:")
folder_label.grid(row=0, column=0, sticky=tk.W)
folder_entry = ttk.Entry(frame, textvariable=folder_path_var, width=50)
folder_entry.grid(row=0, column=1, sticky=(tk.W, tk.E))
folder_button = ttk.Button(frame, text="Browse...", command=browse_folder)
folder_button.grid(row=0, column=2, sticky=tk.E)
save_label = ttk.Label(frame, text="Save Location:")
save_label.grid(row=1, column=0, sticky=tk.W)
save_entry = ttk.Entry(frame, textvariable=save_location_var, width=50)
save_entry.grid(row=1, column=1, sticky=(tk.W, tk.E))
save_button = ttk.Button(frame, text="Browse...", command=choose_save_location)
save_button.grid(row=1, column=2, sticky=tk.E)
preview_label = ttk.Label(frame, text="Preview:")
preview_label.grid(row=2, column=0, sticky=tk.W)
preview_text = tk.Text(frame, height=20, width=80)
preview_text.grid(row=3, column=0, columnspan=3, pady=10)
export_button = ttk.Button(frame, text="Export Tree", command=export_tree)
export_button.grid(row=4, column=0, columnspan=3, pady=10)
# Configure resizing
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
frame.columnconfigure(1, weight=1)
root.mainloop()

Libraries Used:

- `os` (built-in) 

- `tkinter` (built-in) 

- `ttk` (built-in) 

No external dependencies! 

How to Use:

1. Ensure Python 3.x is installed

2. Save the script with the name folder_tree_generator_by_friedx.py

3. Run the script with: python folder_tree_generator_by_friedx.py

4. Use the GUI to select a folder and export the tree! 

 

Virustotal Score
https://www.virustot...TczNDU5MDMxNg==

Let me know your feedback or suggestions! 


  • 0


 Users browsing this thread: and 1 guests