Skip to content

Instantly share code, notes, and snippets.

@WillPapper
Created April 12, 2024 16:39
Show Gist options
  • Save WillPapper/7df10adcc22eb981906c92c115e3ed04 to your computer and use it in GitHub Desktop.
Save WillPapper/7df10adcc22eb981906c92c115e3ed04 to your computer and use it in GitHub Desktop.
Concatenate Markdown Files
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"
# Get a list of Markdown files in the specified folder
markdown_files = [file for file in os.listdir(folder_path) if file.endswith(".md")]
# Open the output file in write mode
with open(output_file, "w") as outfile:
# Iterate over each Markdown file
for file in markdown_files:
# Open the Markdown file in read mode
with open(os.path.join(folder_path, file), "r") as infile:
# Write the filename to the output file
outfile.write(f"# {file}\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