#file_write_example.py

##This program :
##    opens a data file to be written into (test_output.txt)
##    writes several lines into the file
##    closes the file

##The program then:
##    opens the same file
##    reads the lines from it and prints them
##    closes the file


test_file = open("test_output.txt", "wb")

print("\nfile name of output file is :  ",test_file.name)

test_file.write(bytes("first line written to file\n", 'UTF-8'))
test_file.write(bytes("second line written to file\n", 'UTF-8'))
test_file.write(bytes("224.5\n", 'UTF-8'))
test_file.write(bytes("last line written to file\n", 'UTF-8'))

test_file.close()

test_file =open("test_output.txt", "r+")

print("\nLines read from file are : \n")
for line in test_file:
    print(line)
test_file.close()
                      
#another way to read from the file is shown below
                      
test_file =open("test_output.txt", "r+")

print("\nLines read the second way are : \n")

text_in_file = test_file.readline()
print(text_in_file)

text_in_file = test_file.readline()
print(text_in_file)

text_in_file = test_file.readline()
print(text_in_file)

text_in_file = test_file.readline()
print(text_in_file)
                      
test_file.close()  

