A
Austin Songer, CISSP
Guest
- Thread Author
- #1
merge-md.py
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
Show hidden characters
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
Show hidden characters
| 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. |
| |
| |
| :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}") |