Page 200 - Data Science Algorithms in a Week
P. 200

Python Reference


            Reading and writing the file

            The following program will write two lines into the file test.txt, then read them and
            finally print them to the output.
            Input:

                # source_code/appendix_c_python/example15_file.py
                #write to the file with the name "test.txt"
                file = open("test.txt","w")
                file.write("first line\n")
                file.write("second line")
                file.close()
                #read the file
                file = open("test.txt","r")
                print file.read()

            Output:
                $ python example15_file.py
                first line
                second line



































                                                    [ 188 ]
   195   196   197   198   199   200   201   202   203   204   205