Skip to content

Instantly share code, notes, and snippets.

@dkapitan
Created September 19, 2025 05:57
Show Gist options
  • Select an option

  • Save dkapitan/23b9f4299daea6b8504bb1409dbe6c4c to your computer and use it in GitHub Desktop.

Select an option

Save dkapitan/23b9f4299daea6b8504bb1409dbe6c4c to your computer and use it in GitHub Desktop.
Download binary file in memory without saving to disk.
def get_binary(url):
"""Download binary file in memory without saving to disk."""
from io import BytesIO
import requests
try:
response = requests.get(url)
# Raise an exception if the request was unsuccessful (e.g., 404 Not Found)
response.raise_for_status()
# Get the binary content of the response
file_content = response.content
# Create an in-memory binary stream from the file content using BytesIO
# This allows polars to read the data as if it were a file on disk
file_buffer = BytesIO(file_content)
# Read the Excel data from the buffer into a Polars DataFrame
# The `read_excel` function can accept a file path or a file-like object
df = pl.read_excel(file_buffer)
return df
except requests.exceptions.RequestException as e:
print(f"An error occurred during download: {e}")
return None
except Exception as e:
print(f"An error occurred while processing the file: {e}")
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment