forked from jackdoe/programming-for-kids
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.py
34 lines (31 loc) · 1.03 KB
/
extract.py
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
#! env
from os import listdir, makedirs
from os.path import isfile, join
for fn in listdir('.'):
if isfile(fn) and fn.endswith('.md') and fn.startswith('week-0'):
print("extracting",fn)
path = join(".","examples",fn.replace('.md',''))
try:
makedirs(path)
except FileExistsError:
pass
f = open(fn,"r")
lines = f.readlines()
f.close()
id = 0
current = None
for line in lines:
if line == '```\n' or line == '```': # in case it ends without new line
if current == None:
id += 1
current_name = join(path, "{:03d}.txt".format(id))
current = open(current_name,"w")
print("openning",current_name)
else:
current.close()
current = None
elif current != None:
current.write(line)
if current != None:
current.close()
current = None