Define function to Open a file : IO Error « File « Python






Define function to Open a file

Define function to Open a file
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("\n\nPress the enter key to exit.")
        sys.exit()
    else:
        return the_file

trivia_file = open_file("trivia.txt", "r")


           
       








Related examples in the same category

1.Define file open and read in a functionDefine file open and read in a function
2.Handle File reading ExceptionHandle File reading Exception