get_bytes_range()#

cached_path.get_bytes_range(url_or_filename, index, length, cache_dir=None, extract_archive=False, force_extract=False, quiet=False, progress=None)[source]#

Get a range of up to length bytes starting at index.

In some cases the entire file may need to be downloaded, such as when the server does not support a range download or when you’re trying to get a bytes range from a file within an archive.

Caution

You may get less than length bytes sometimes, such as when fetching a range from an HTTP resource starting at 0 since headers will be omitted in the bytes returned.

Parameters:
  • url_or_filename (Union[str, PathLike]) – A URL or path to parse and possibly download.

  • index (int) – The index of the byte to start at.

  • length (int) – The number of bytes to read.

  • cache_dir (Union[PathLike, str, None], default: None) –

    The directory to cache downloads. If not specified, the global default cache directory will be used (~/.cache/cached_path). This can be set to something else with set_cache_dir().

    This is only relevant when the bytes range cannot be obtained directly from the resource.

  • extract_archive (bool, default: False) –

    Set this to True when you want to get a bytes range from a file within an archive. In this case the url_or_filename must contain an “!” followed by the relative path of the file within the archive, e.g. “s3://my-archive.tar.gz!my-file.txt”.

    Note that the entire archive has to be downloaded in this case.

  • force_extract (bool, default: False) –

    If True and the resource is a file within an archive (when the path contains an “!” and extract_archive=True), it will be extracted regardless of whether or not the extracted directory already exists.

    Caution

    Use this flag with caution! This can lead to race conditions if used from multiple processes on the same file.

  • quiet (bool, default: False) –

    If True, progress displays won’t be printed.

    This is only relevant when the bytes range cannot be obtained directly from the resource.

  • progress (Optional[Progress], default: None) –

    A custom progress display to use. If not set and quiet=False, a default display from get_download_progress() will be used.

    This is only relevant when the bytes range cannot be obtained directly from the resource.

Return type:

bytes