Skip to content

Instantly share code, notes, and snippets.

@alvivar
Created October 3, 2024 03:11
Show Gist options
  • Save alvivar/2cb816f3a1d2f3a75fe111824bed769f to your computer and use it in GitHub Desktop.
Save alvivar/2cb816f3a1d2f3a75fe111824bed769f to your computer and use it in GitHub Desktop.
Concatenates files with specified extension into a single file. Usable to send complete code bases to LLMs.
import glob
import os
import sys
def concatenate_files(extension):
current_dir = os.path.dirname(os.path.abspath(__file__))
files = glob.glob(os.path.join(current_dir, f"**/*.{extension}"), recursive=True)
output_file = os.path.join(current_dir, "concat.txt")
with open(output_file, "w") as outfile:
for file in files:
relative_path = os.path.relpath(file, current_dir)
outfile.write(f"# {relative_path}\n\n")
with open(file, "r") as infile:
outfile.write(infile.read())
outfile.write("\n\n")
print(f"All {extension} files have been concatenated into '{output_file}'")
if __name__ == "__main__":
if len(sys.argv) != 2:
usage_message = "Usage: python concat.py <file_extension>\n"
result_message = (
"Result: Concatenates all files with the specified extension\n"
" in the current directory and its subdirectories\n"
" into a single file named 'concat.txt'"
)
print(usage_message)
print(result_message)
sys.exit(1)
file_extension = sys.argv[1]
concatenate_files(file_extension)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment