def get_file_obj(fname, mode='r', encoding=None):
    """
    Light wrapper to handle strings and let files (anything else) pass through.

    It also handle '.gz' files.

    Parameters
    ==========
    fname: string or file-like object
        File to open / forward
    mode: string
        Argument passed to the 'open' or 'gzip.open' function
    encoding: string
        For Python 3 only, specify the encoding of the file

    Returns
    =======
    A file-like object that is always a context-manager. If the `fname` was already a file-like object,
    the returned context manager *will not close the file*.
    """
    if _is_string_like(fname):
        return _open(fname, mode, encoding)
    try:
        # Make sure the object has the write methods
        if 'r' in mode:
            fname.read
        if 'w' in mode or 'a' in mode:
            fname.write
    except __HOLE__:
        raise ValueError('fname must be a string or a file-like object')
    return EmptyContextManager(fname)
dataset/ETHPy150Open statsmodels/statsmodels/statsmodels/iolib/openfile.py/get_file_obj