Define file open and read in a function : IO Error : File PYTHON examples


PYTHON examples » File » IO Error »

 

Define file open and read in a function


Define file open and read in a function

import sys
       
def open_file(file_name, mode):
    """Open a file."""
    try:
        the_file = open(file_name, mode)
    except(IOError), e:
        print "Unable to open the file", file_name, "Ending program.n", e
        raw_input("nnPress the enter key to exit.")
        sys.exit()
    else:
        return the_file


def next_line(the_file):
    """Return next line from the trivia file, formatted."""
    line = the_file.readline()
    line = line.replace("/""n")
    return line

trivia_file = open_file("trivia.txt""r")
title = next_line(trivia_file)

print title
           
       



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON examples

 Navioo File
» IO Error