(
    filepath,
    mode,
    *,
    compression=None,
    encoding=None,
    errors=None,
    newline=None,
)
return f
Get a file handle for the given ``filepath`` and ``mode``, plus optional kwargs.
Get a file handle for the given ``filepath`` and ``mode``, plus optional kwargs.
Get a file handle for the given filepath and mode plus optional kwargs .
def _get_file_handle(
    filepath,
    mode,
    *,
    compression=None,
    encoding=None,
    errors=None,
    newline=None,
):
    """
    Get a file handle for the given ``filepath`` and ``mode``, plus optional kwargs.
    """
    if compression:
        mode_ = mode.replace("b", "").replace("t", "")
        if compression == "gzip":
            f = gzip.GzipFile(filepath, mode=mode_)
        elif compression == "bz2":
            f = bz2.BZ2File(filepath, mode=mode_)
        elif compression == "xz":
            f = lzma.LZMAFile(filepath, mode=mode_)
        elif compression == "zip":
            zip_file = zipfile.ZipFile(filepath, mode=mode_)
            zip_names = zip_file.namelist()
            if len(zip_names) == 1:
                f = zip_file.open(zip_names[0])
            elif len(zip_names) == 0:
                raise ValueError(f"no files found in zip file '{filepath}'")
            else:
                raise ValueError(
                    f"{len(zip_names)} files found in zip file '{filepath}', "
                    "but only one file is allowed"
                )
        else:
            valid_values = [None, "infer"] + sorted(_ext_to_compression.values())
            raise ValueError(
                errors_.value_invalid_msg("compression", compression, valid_values)
            )

        if "t" in mode:
            f = io.TextIOWrapper(f, encoding=encoding, errors=errors, newline=newline)

    # no compression, file is opened as usual
    else:
        f = filepath.open(mode=mode, encoding=encoding, errors=errors, newline=newline)

    return f
def _get_file_handle ( filepath , mode , * , compression = None , encoding = None , errors = None , newline = None , ) : if compression : mode_ = mode . replace ( "b" , "" ) . replace ( "t" , "" ) if compression == "gzip" : f = gzip . GzipFile ( filepath , mode = mode_ ) elif compression == "bz2" : f = bz2 . BZ2File ( filepath , mode = mode_ ) elif compression == "xz" : f = lzma . LZMAFile ( filepath , mode = mode_ ) elif compression == "zip" : zip_file = zipfile . ZipFile ( filepath , mode = mode_ ) zip_names = zip_file . namelist ( ) if len ( zip_names ) == 1 : f = zip_file . open ( zip_names [ 0 ] ) elif len ( zip_names ) == 0 : raise ValueError ( f"no files found in zip file '{filepath}'" ) else : raise ValueError ( f"{len(zip_names)} files found in zip file '{filepath}', " "but only one file is allowed" ) else : valid_values = [ None , "infer" ] + sorted ( _ext_to_compression . values ( ) ) raise ValueError ( errors_ . value_invalid_msg ( "compression" , compression , valid_values ) ) if "t" in mode : f = io . TextIOWrapper ( f , encoding = encoding , errors = errors , newline = newline ) # no compression, file is opened as usual else : f = filepath . open ( mode = mode , encoding = encoding , errors = errors , newline = newline ) return f