Created
May 7, 2024 16:34
-
-
Save WillPapper/69fb0dce266eaec5c614c99fd4306676 to your computer and use it in GitHub Desktop.
Recursively Concatenate Markdown
This file contains 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
import os | |
# Specify the folder path containing the Markdown files | |
folder_path = "./path-goes-here" | |
# Specify the output file path | |
output_file = "concatenated_markdown.md" | |
# Open the output file in write mode | |
with open(output_file, "w") as outfile: | |
# Iterate over each directory and subdirectory using os.walk() | |
for root, dirs, files in os.walk(folder_path): | |
# Get a list of Markdown files in the current directory | |
markdown_files = [file for file in files if file.endswith(".md")] | |
# Iterate over each Markdown file | |
for file in markdown_files: | |
# Open the Markdown file in read mode | |
with open(os.path.join(root, file), "r") as infile: | |
# Write the relative path to the file as a heading | |
relative_path = os.path.relpath(os.path.join(root, file), folder_path) | |
outfile.write(f"# {relative_path}\n\n") | |
# Write the contents of the Markdown file to the output file | |
outfile.write(infile.read()) | |
# Add a line break between files | |
outfile.write("\n\n---\n\n") | |
print("Concatenation complete. Output file:", output_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment