-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.py
More file actions
37 lines (26 loc) · 933 Bytes
/
Copy pathsplit.py
File metadata and controls
37 lines (26 loc) · 933 Bytes
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
import os
import re
os.chdir("personas")
files = [f for f in os.listdir(".") if f.endswith(".txt")]
for file in files:
with open(file, "r") as f:
content = f.read()
# Extract Character Name
name_match = re.search(r"# NAME:\s*(.+)", content)
if not name_match:
continue
char_name = name_match.group(1).strip().replace(" ", "_")
# Create directory
os.makedirs(char_name, exist_ok=True)
# Split by ### headers
sections = re.split(r"###\s+(.+)", content)
for i in range(1, len(sections), 2):
header = sections[i].strip().replace(" ", "_")
body = sections[i+1].strip()
# Determine filename
filename = f"{header}.txt"
with open(os.path.join(char_name, filename), "w") as out_f:
out_f.write(body)
# Remove original unified file
os.remove(file)
print("Split complete.")