import gzip
import bz2


def get_file_handle_by_type(file_name, _mode, _type):
    """Open a file according to its mode and return the handle
    """

    _handle = None
    _type = int(_type)
    if _type == 0:
        _handle = open(file_name, _mode)
    elif _type == 1:
        _handle = gzip.open(file_name, _mode)
    elif _type == 2:
        _handle = bz2.BZ2File(file_name, _mode)
    return _handle