from pathlib import Path from collections import defaultdict index lin

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from pathlib import Path
from collections import defaultdict
index = 1
lines = defaultdict(list)
with open("data_with_separator", "rb") as f:
for line in f:
if b">>>>" in line:
index += 1
continue
key = f"data{index}"
lines[key].append(line)
for name in lines:
old_file = Path(name)
if old_file.is_file():
raise ValueError(f"old file '{name}' already exists")
with open(name, "wb") as f:
f.write(b"".join(lines[name]))