Skip to content

Instantly share code, notes, and snippets.

@austinsonger
Last active January 2, 2025 20:39
Show Gist options
  • Save austinsonger/ccdc7d38a31f961d6c5415721384f756 to your computer and use it in GitHub Desktop.
Save austinsonger/ccdc7d38a31f961d6c5415721384f756 to your computer and use it in GitHub Desktop.
Merges all Markdown files found in the root directory and its subdirectories into a single output file.
import os
import glob
import argparse
# Define a default root directory constant
DEFAULT_ROOT_DIR = "<PLACEHOLDER>"
def merge_markdown_files(root_dir, output_file):
"""
Merges all Markdown files found in the root directory and its subdirectories into a single output file.
:param root_dir: The root directory to search for Markdown files.
:param output_file: The path of the output file where the merged content will be stored.
:raises FileNotFoundError: If root_dir does not exist
"""
if not os.path.exists(root_dir):
raise FileNotFoundError(f"Directory '{root_dir}' does not exist")
markdown_files = []
# Walk through all directories and subdirectories
for dirpath, dirnames, filenames in os.walk(root_dir):
for filename in filenames:
if filename.endswith(".md"):
# Append the full path of files ending with .md
markdown_files.append(os.path.join(dirpath, filename))
# Sort files for consistent order (optional)
markdown_files.sort()
# Writing all markdown content to the output file
with open(output_file, 'w', encoding='utf-8') as outfile:
for md_file in markdown_files:
try:
with open(md_file, 'r', encoding='utf-8') as infile:
content = infile.read()
outfile.write(content + '\n\n')
except UnicodeDecodeError:
print(f"Warning: Skipping file {md_file} due to encoding issues")
continue
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Merge Markdown files into a single output file.")
parser.add_argument('-p', '--path', type=str, help='Directory path to search for Markdown files', default=DEFAULT_ROOT_DIR)
parser.add_argument('-o', '--output', type=str, help='Output file path', default='merged_output.md')
args = parser.parse_args()
try:
merge_markdown_files(args.path, os.path.join(args.path, args.output))
print(f"Successfully merged markdown files into '{args.output}'")
except Exception as e:
print(f"Error: {e}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment