Last active
October 25, 2021 17:37
-
-
Save kwinkunks/df1c52dd35e60ac498947a154111e2ba to your computer and use it in GitHub Desktop.
Read a file from the Internet to give to an arbitrary function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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