infer_extension#
- scikitplot.corpus.infer_extension(headers, url)[source]#
Infer a file extension from HTTP response headers and URL path.
Public wrapper around
_infer_extension_from_headers. Call this when you already hold response headers (e.g. after a manualrequests.head()) and want to know what extension to use for the downloaded file.The resolution order is:
URL path extension (cheapest, most reliable when present).
Content-Dispositionfilename*=(RFC 5987 encoded form).Content-Dispositionfilename=(plain form).Content-TypeMIME mapping (skipsapplication/octet-stream).mimetypes.guess_extensionstdlib fallback.".bin"when nothing can be inferred.
- Parameters:
- headersdict-like or http.client.HTTPMessage
HTTP response headers that support
.get(key, default).- urlstr
Original request URL. Used for path-based extension lookup and as a logging label.
- Returns:
- str
File extension including leading dot, e.g.
".pdf". Returns".bin"if nothing can be inferred.
- Parameters:
- Return type:
Examples
>>> infer_extension({"Content-Type": "audio/mpeg"}, "https://host/dl") '.mp3' >>> infer_extension({}, "https://host/report.pdf") '.pdf' >>> infer_extension( ... {"Content-Disposition": "attachment; filename*=UTF-8''report%20final.pdf"}, ... "https://host/dl", ... ) '.pdf'