-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_into_md.py
More file actions
43 lines (37 loc) · 1.1 KB
/
Copy pathmerge_into_md.py
File metadata and controls
43 lines (37 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
"""
Merge the content of all files from the given folder recursively into a single .md file.
"""
from pathlib import Path
import argparse
def main():
parser = argparse.ArgumentParser(
description="Merge all files recursively into a single file."
)
parser.add_argument(
"folder", type=str, help="Path to the folder containing the files."
)
parser.add_argument(
"-o",
"--output",
type=str,
help="Output markdown file (default: FOLDER_NAME.md).",
)
args = parser.parse_args()
base = Path(args.folder)
if args.output:
out = Path(args.output)
else:
out = base.with_suffix(".md")
with out.open("w", encoding="utf-8") as fout:
for p in sorted(base.rglob("*")):
if not p.is_file():
continue
fout.write(f"\n\n# [File]: {p.relative_to(base)}\n\n")
try:
text = p.read_text(encoding="utf-8")
except Exception:
text = "UNREADABLE"
fout.write(text)
if __name__ == "__main__":
main()