Last active
August 17, 2025 03:19
-
-
Save stuaxo/f1beb981dc4845921c31fcb4e16f4821 to your computer and use it in GitHub Desktop.
Output files in subdirectories for ingestion to an LLM such as Claude, ChatGPT etc.
This file contains hidden or 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
#!/usr/bin/env python3 | |
# Usage: python dirtogpt.py [--dir /path/to/directory] [--glob *.py] [--prompt [Custom prompt]] [--copy] | |
import argparse | |
import pathlib | |
try: | |
import pyperclip | |
except ImportError: | |
pyperclip = None | |
def append_file_content(output, path): | |
output += f"#:{path}:\n{path.read_text()}\n\n" | |
return output | |
def dirtogpt(output, directory, glob): | |
file_count = 0 | |
p = pathlib.Path(directory) | |
for child in p.glob(glob): | |
if child.is_file(): | |
output = append_file_content(output, child) | |
file_count += 1 | |
elif child.is_dir(): | |
output, sub_file_count = dirtogpt(output, child, glob) | |
file_count += sub_file_count | |
return output, file_count | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Process some files.') | |
parser.add_argument('--dir', type=str, help='Directory to process', default=".") | |
parser.add_argument('--glob', type=str, help='Glob pattern to match', default="**/*") | |
parser.add_argument('--prompt', nargs='?', const="Filenames followed by file content-:", default=None, help='Display a prompt before the files') | |
parser.add_argument('--copy', action='store_true', help='Copy output to the clipboard instead of stdout') | |
args = parser.parse_args() | |
output = "" | |
if args.prompt is not None: | |
output += args.prompt + "\n" | |
output, file_count = dirtogpt(output, args.dir, args.glob) | |
token_count = len(output.split()) | |
if args.copy: | |
if pyperclip: | |
pyperclip.copy(output) | |
print(f"Copied {file_count} files, {len(output)} bytes, and approximately {token_count} tokens to the clipboard.") | |
else: | |
print("The --copy option requires the 'pyperclip' module. Please install it to use this functionality.") | |
else: | |
print(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very cool dear.