You may want a code something like –
start = 1
lines = ['Step' + str(start) + ':\n']
with open('file.txt','r') as inF:
prevspace = -1
for line in inF:
lspaces = len(line) - len(line.lstrip())
if lspaces > prevspace and prevspace != -1:
lines.append('Step' + str(start+1) + ':\n')
start = start + 1
lines.append(line)
prevspace = lspaces
else:
lines.append(line)
prevspace = lspaces
ifF.close()
with open('newfile.txt','w') as outF:
for line in lines:
outF.write(line)
outF.flush()
outF.close()
4
solved Python – Put new line into file at largest indent