Skip to content

Instantly share code, notes, and snippets.

@stuaxo
Last active August 17, 2025 03:19
Show Gist options
  • Save stuaxo/f1beb981dc4845921c31fcb4e16f4821 to your computer and use it in GitHub Desktop.
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.
#!/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, file_count = dirtogpt(output, args.dir, args.glob)
token_count = len(output.split())
if args.prompt is not None:
output = args.prompt + "\n" + output
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)
@amigax
Copy link

amigax commented May 21, 2024

very cool dear.

@stuaxo
Copy link
Author

stuaxo commented May 21, 2024

very cool dear.

🤣

@stuaxo
Copy link
Author

stuaxo commented Sep 10, 2024

@amigax now show me your batch files, hehe 😋

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment