Skip to content

Instantly share code, notes, and snippets.

@ahmadrosid
Last active February 28, 2024 07:52
Show Gist options
  • Save ahmadrosid/229b462debe8a594f4bca0ca41fc7c8a to your computer and use it in GitHub Desktop.
Save ahmadrosid/229b462debe8a594f4bca0ca41fc7c8a to your computer and use it in GitHub Desktop.
Read entire folder file and append into one file, currently for python file only. This is good to feed LLM to talk with your code.
import os
def write_directory_structure(directory, file, ignore_folders, indent=""):
for item in os.listdir(directory):
item_path = os.path.join(directory, item)
if os.path.isdir(item_path):
if item not in ignore_folders:
file.write(indent + "|-- " + item + "/\n")
write_directory_structure(item_path, file, ignore_folders, indent + "| ")
else:
print(f"Ignoring folder: {item}")
else:
if item.endswith('.py'):
file.write(indent + "|-- " + item + "\n")
def append_files_to_one(directory, output_file, ignore_folders):
with open(output_file, 'a', encoding='utf-8') as output:
output.write(directory + "/\n")
output.write("|-- " + directory + "/\n")
write_directory_structure(directory, output, ignore_folders)
output.write("\n")
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.py'):
filepath = os.path.join(root, file)
with open(filepath, 'r', encoding='utf-8') as f:
output.write("\nFile: " + file + "\n")
output.write(f.read() + "\n")
directory_to_process = "path/target/folder"
# Provide the output file name
output_file = "output.txt"
# Specify folders to ignore
ignore_folders = {'locale', 'docs', 'migrations', 'tests', 'static', '__pycache__', 'templates'}
append_files_to_one(directory_to_process, output_file, ignore_folders)
print("Files appended successfully!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment