Writing to text files

The mechanics of writing to text files are very straightforward. Suppose we need to store a list of coordinates to a text file, e.g.

coords = [[12, 31], [75, 19], [28, 51]]

Writing them to a file could be done by code like this,

# write_coords.py
coords = [[12, 31], [75, 19], [28, 51]]
fname = input('Name of file to create? ')
f = open(fname, 'w')
for coord in coords:
    f.write(str(coord[0]) + ' ' + str(coord[1]) + '\n')
f.close()

The pattern is similar to reading from a file:

The resulting file looks like this if you open it in a text editor, e.g. IDLE,

12 31
75 19
28 51