修改一下
step = 100
f = open('r.txt','w')
ls = open('a.txt','r').readlines()
header = ''
content = ''
ls.append('#####\n') # add mark of file end
for row, l in enumerate(ls):
if l.startswith('#'): # encounter new header
if content != '':
f.write(header) # write header
for i in range((len(content) - 1) / step + 1): # zip content
f.write(content[i*step : i*step + step] + '\n') # write content
content = '' # reset content
header = ('\n' if row > 0 else '') + l # write header
else:
content += l.replace('\n','') # add new line to content