11. # incr.py
n = 0
print('n = %d' % n) #
with open('incr.py') as f:
lines = f.read().split('n')
for idx, l in enumerate(lines):
if l.startswith('n = '):
next_n = int(l.replace('n = ', '')) + 1
lines[idx] = 'n = %d' % next_n
with open('incr.py', 'w') as f:
f.write('n'.join(lines))
12. $ python3 incr.py
n = 0
$ python3 incr.py
n = 1
$ python3 incr.py
n = 2
$ python3 incr.py
n = 3
$ python3 incr.py
n = 4