def main():
    openOk = False
    # The flag starts out false, so the loop runs at least once.
    while not openOk:
        try:
            # Get a filename and try to open it
            fn = input("Enter a file name: ")
            infile = open(fn, "r")
            # If we got here, open succeeded.
            openOk = True
        except IOError:
            print("Could not open", fn)
   
    # If we got here, we know it successfully opened.
    # Now fn is the filename and infile the file object.

    # Label and print the lines of the file.
    lineno = 1
    for line in infile:
        # Remove newline (and other trailing whitespace)
        line = line.rstrip()
        print(fn, "line", lineno, ":", line)
        lineno += 1

    # Free the resources associated with the file object.
    infile.close()

main()

