1# with is like your try .. finally block in this case
2with open('stats.txt', 'r') as file:
3 # read a list of lines into data
4 data = file.readlines()
5
6print data
7print "Your name: " + data[0]
8
9# now change the 2nd line, note that you have to add a newline
10data[1] = 'Mage\n'
11
12# and write everything back
13with open('stats.txt', 'w') as file:
14 file.writelines( data )
1# with is like your try .. finally block in this case
2with open('stats.txt', 'r') as file:
3 # read a list of lines into data
4 data = file.readlines()
5
6print data
7print "Your name: " + data[0]
8
9# now change the 2nd line, note that you have to add a newline
10data[1] = 'Mage\n'
11
12# and write everything back
13with open('stats.txt', 'w') as file:
14 file.writelines( data )
15