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
")
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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset