import codecs


def open_with_encoding(filename, encoding, mode='r'):
    """Return opened file with a specific encoding."""
    try:
        # Python 3
        return open(filename, mode=mode, encoding=encoding)
    except TypeError:
        # Python 2
        return codecs.open(filename, mode=mode, encoding=encoding)