Add mode param to download_text() to allow using custom modes like 'rb'

This commit is contained in:
Dan Streetman 2021-05-28 16:14:51 -04:00
parent 128eca1a5b
commit ff66707a4c

View File

@ -334,18 +334,20 @@ def download(src, dst, size=0):
size / 1024.0 / 1024))
def download_text(src):
def download_text(src, mode='r'):
""" return the text content of a downloaded file
src: str
Source to copy from (file path or url)
mode: str
Mode to use with open()
Raises the same exceptions as download()
Returns text content of downloaded file
Returns text (or binary, if mode includes 'b') content of downloaded file
"""
with tempfile.TemporaryDirectory() as d:
dst = os.path.join(d, 'dst')
download(src, dst)
with open(dst) as f:
with open(dst, mode=mode) as f:
return f.read()