Skip to content

Instantly share code, notes, and snippets.

@ahmedelgabri
Forked from iguatemigarin/concat.sh
Created July 5, 2024 13:42
Show Gist options
  • Save ahmedelgabri/9bb889c464714eae452f953f30287216 to your computer and use it in GitHub Desktop.
Save ahmedelgabri/9bb889c464714eae452f953f30287216 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import argparse
import sys
def combine_markdown_files(folder_path, output_file, max_size):
current_size = 0
try:
with open(output_file, 'w', encoding='utf-8') as outfile:
for root, dirs, files in os.walk(folder_path, followlinks=False):
for file in files:
if file.endswith('.md'):
filepath = os.path.join(root, file)
outfile.write(f"<file path=\"{filepath}\">\n\n")
try:
with open(filepath, 'r', encoding='utf-8') as infile:
content = infile.read()
outfile.write(content)
outfile.write('\n\n')
outfile.write(f"</file>\n\n")
current_size += len(content)
if current_size > max_size:
print(f"Reached size limit at {filepath}")
return
except IOError as e:
print(f"Error reading file {filepath}: {e}", file=sys.stderr)
print(f"Combined Markdown files written to {output_file}")
except IOError as e:
print(f"Error writing to output file {output_file}: {e}", file=sys.stderr)
def main():
parser = argparse.ArgumentParser(description="Combine Markdown files from a folder into a single file with a size limit.")
parser.add_argument("folder_path", help="Path to the folder containing Markdown files")
parser.add_argument("-o", "--output", default="combined.md", help="Output file name (default: combined.md)")
parser.add_argument("-s", "--size", type=int, default=5, help="Maximum size limit in GB (default: 5)")
args = parser.parse_args()
if not os.path.isdir(args.folder_path):
print(f"Error: '{args.folder_path}' is not a valid directory.", file=sys.stderr)
sys.exit(1)
max_size = 1024 * 1024 * 1024 * args.size # Convert GB to bytes
combine_markdown_files(args.folder_path, args.output, max_size)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment