<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def loadfile(path, mode=None, filetype=None, **kwargs):
 """Loads the given file using the appropriate InferenceFile class. If ``filetype`` is not provided, this will try to retreive the ``filetype`` from the file's ``attrs``. If the file does not exist yet, an IOError will be raised if ``filetype`` is not provided. Parameters path : str The filename to load. mode : str, optional What mode to load the file with, e.g., 'w' for write, 'r' for read, 'a' for append. Default will default to h5py.File's mode, which is 'a'. filetype : str, optional Force the file to be loaded with the given class name. This must be provided if creating a new file. Returns ------- filetype instance An open file handler to the file. The class used for IO with the file is determined by the ``filetype`` keyword (if provided) or the ``filetype`` stored in the file (if not provided). """

    if filetype is None:
        # try to read the file to get its filetype
        try:
            fileclass = get_file_type(path)
        except IOError:
            # file doesn't exist, filetype must be provided
            raise IOError("The file appears not to exist. In this case, "
                          "filetype must be provided.")
    else:
        fileclass = filetypes[filetype]
    return fileclass(path, mode=mode, **kwargs)