# Arup Guha
# 11/17/2020
# Basic File Reading Example just by line.
# Edits to first example: check for invalid file, and we will learn to strip
# away the \n at the end of a line.

import os.path
from os import path

# Get the file name.
filename = input("What is the name of your file?\n")

if path.exists(filename):

    # This opens the file with the name filename and gets it ready for us
    # to read from.
    file = open(filename, "r")

    # readline will read the whole line, including the \n and return that as a
    # string. Once the line is read, the file pointer (bookmark) is advanced to
    # the next line.
    numLines = int(file.readline())

    # Go through each line.
    for loop in range(numLines):

        # .rstrip, when called on a string gets rid of all the white space at the
        # right end of the string.
        myline = file.readline().rstrip()

        # Now I do print a newline by default since myline doesn't have it anymore.
        print("Just read in :", myline)

    # Close file
    file.close()

# Error message for no file.
else:
    print("Sorry, file not found.")
