Computational Models with Arithmetic Growth 187
1 # writing to a data file
2 mfile = open("mtest.dat", ’w’)
3 mfile.write("Testing output data\n")
4 v1 = 56
5 v1str = str(v1)
6 mfile.write(v1str)
7 v2 = 10.45
8 v2str = str(v2)
9 mfile.write("\n")
10 mfile.write(v2str + "\n")
11 mfile.write(v1str + " " + v2str +"\n")
12 mfile.close()
The following lines are produced when the Python interpreter runs the
program.
Testing output data
56
10.45
56 10.45
12.7.4 Reading Data from a File
As mentioned previously, a text file stores data in string for m. Reading data
from a text file involves reading lines of text from a file. Method read reads a
string fro m an open file. This method starts reads from the beginning of the
file and tries to read as much as possible, or until ...