#!/usr/bin/env python
#=========================================================================================
# many different ways to read from a file
#=========================================================================================
 
print("Content-Type: text/html \n")          #required http response header (w/ extra line)

fileIn  = '/home/sultans/data/cust_order.file'   	#input file

line   = ''                                          #a string that will contain a single line
string = ''                                          #a string that will contain entire file
array  = []                                          #a single dimension array to store lines
array2 = []                                          #2 dimensional array to store each data element 

#========================================================================================
# read_file_into_string: read entire file into a string
#========================================================================================
def read_file_into_string(fileIn, string):
 
    input = open(fileIn)                                #Open file for reading - default 'r'    
    string = input.read()                               #read entire file into a string                
    input.close()                                       #close the file

    print(string)                                       #print entire file

#========================================================================================
# read_file_into_string2: read entire file into a string (another way)
#========================================================================================
def read_file_into_string2(fileIn, string):
 
    with open(fileIn) as input :                        #Open file for reading - default 'r'    
        string = input.read()                           #read entire file into a string                
#       input.close()                                   #close is automatic

    print(string)                                       #print entire file

#========================================================================================
# read_line_into_string: read each line from file into a string
# preferred method 
#========================================================================================
def read_line_into_string(fileIn, line):
 
    with open(fileIn) as input:                         #Open file for reading - default 'r'    
        for line in input:                              #iterate thru the file handle
            line = line.rstrip()                        #strip trailing newline
            print(line)                                 #print line
#       input.close()                                   #done automatically

#========================================================================================
# read_line_into_string2: read each line from file into a string (another way)
# not preferred method
#========================================================================================
def read_line_into_string2(fileIn, line):
 
    with open(fileIn) as input:                         #Open file for reading - default 'r'    
        line = input.readline()                         #read single line into a string
        while line:                
            line = line.rstrip()                        #strip trailing newline
            print(line)                                 #print line
            line = input.readline()                     #read next single line into a string
#       input.close()                                   #done automatically

#========================================================================================
# read_file_into_array: read entire file into a single dimensional array
#========================================================================================
def read_file_into_array(fileIn, array):
 
    input = open(fileIn,'r')                            #Open file for reading    
    array = input.readlines()                           #read entire file into a list
    input.close()                                       #close the file

    for line in array:                                  #loop through the array        
        line = line.rstrip()                            #strip trailing newline
        print(line)                                     #print each line
                         

####################################################################################
# main code
####################################################################################
print('READ ENTIRE FILE INTO STRING ==================================')
read_file_into_string(fileIn, string) 

print('READ ENTIRE FILE INTO STRING (another way) ====================')
read_file_into_string2(fileIn, string)          

print('READ A SINGLE LINE INTO STRING ================================')
read_line_into_string(fileIn, line) 

print('\nREAD A SINGLE LINE INTO STRING (another way) ================')
read_line_into_string2(fileIn, line)             

print('\nREAD ENTIRE FILE INTO ARRAY =================================')
read_file_into_array(fileIn, array)