# testio.py print "testio.py write then read file testio.dat" f=open("testio.dat", "w") # open for writing f.write("line 1\n") # no line feed, \ n, unless you use it f.write("line 2\n") f.close # be nice, close any file you open print "wrote and closed testio.dat" f=open("testio.dat", "r") # open for reading print "read and print testio.dat one line at a time" a=f.readline() # f is the file handle while a != "": # null returned for end of file a=a[:-1] # remove \ n to avoid extra blank line print a a=f.readline() print a # should be a null string print "expect eof yet just reads null strings" a=f.readline() print a # printing null string is blank line on dispaly a=f.readline() print a a=f.readline() print a f.close f=open("testio.dat", "r") print "again,read and print testio.dat" while True: a=f.readline() if not a: # not the null string is true break a=a[:-1] print a f.close f=open("testio.dat", "r") print "read entire file into a, print with one statement" a=f.read() print "print entire file, one long string, with line feeds" print a f.close