#file_read_example.py

##This program opens a data file, reads in each line of the file and prints it.
##Each line that is read in is terminated by an end-of-line character "\n"

##The program then closes the data file and reopens it (so that it will start
##reading with the first line), reads in each line, removes the trailing "\n"
##and prints the line.

##The data file should be saved with the name "data_1.txt" in the same
##folder as the program.   I suggest the following contents for the data file,
##though the program should work with any text contents:

##first line in data file
##second line in data file
##another line
##the next line is a number
##28
##last line in data file

# a data_1.txt file is available with this example

import random
import sys
import os

test_file =open("data_1.txt", "r+")  #opens file
       # the opened file will be referred to as "test_file" in
       # program statements

print("\nHere are the lines read from the file :\n")

count = 1
for line in test_file:  #reads in each line from file
        print("line# ",count," : ",line)
        count = count + 1
test_file.close() #closes the data_1.txt file

print("\nHere are the same lines without the trailing \"\\n\n")

test_file =open("data_1.txt", "r+")  #re_opens the data_1.txt file

count = 1
for line in test_file:
        line = line.strip()  #removes trailing "\n"e.close() 
        print("line #",count," : ",line)
        count = count + 1
test_file.close() 
        
