Skip to content

Instantly share code, notes, and snippets.

@kwinkunks
Last active October 25, 2021 17:37
Show Gist options
  • Save kwinkunks/df1c52dd35e60ac498947a154111e2ba to your computer and use it in GitHub Desktop.
Save kwinkunks/df1c52dd35e60ac498947a154111e2ba to your computer and use it in GitHub Desktop.
Read a file from the Internet to give to an arbitrary function
import re
import urllib
from io import BytesIO
# If the fname you gave me looks like a URL...
if re.match(r'https?://.+\..+/.+?', fname) is not None:
try:
data = urllib.request.urlopen(fname).read()
except urllib.error.HTTPError as e:
raise IOError('Could not retrieve url: ', e)
fname = BytesIO(data)
# This next bit is made up code:
my_data = my_library.read_file(fname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment