algorithmiaio/algorithmia-python

how to download file to local disk?

Closed this issue · 3 comments

CayoM commented

The example of the api docs for python looks like:
t800File = client.file("data://.my/robots/T-800.png").getFile()

but no matter how i try to save the response I keep getting errors.

if i try to read the response in order to write to a file, i get this error:

    with open("test.png", "w") as f:
        f.write(response.read())

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

if i try to save the response directly, i get this:

    with open("test.png", "w") as f:
        f.write(response)

TypeError: write() argument must be str, not _io.TextIOWrapper

Any help is appreciated.

Hi @CayoM ! You’ll need to convert your TextIOWrapper to a string first; see https://stackoverflow.com/a/43438389

CayoM commented

thanks for the hint. I managed to download the file now.

Here is my snippet in case someone wants to update the official docs:

def download_file(algorithmia_file_name):
    response = client.file(f"{algorithmia_thumbnail_path}/{algorithmia_file_name}").getFile()

    with open(response.name, 'rb') as f2:
        data = f2.read()
        with open("test.png", "wb") as f:
            f.write(data)
            f.close()

tagging @besirkurtulmus for docs followup if applicable ^