Skip to content

[Function request] A way to download tarfiles. #238

@ggavelis

Description

@ggavelis

Hello and thanks for your immensely useful library!

I've found .get_file_contents() to be especially useful. Do you have any plans to make a way to download non-string type data. (i.e. contents for tarfiles?)

I'm a novice, but have tried tweaking .get_file_contents() to work on tarfiles.
The only difference with my code (at end of this messsage) is that

  • it makes a request (r) and returns r.raw

whereas your code, .get_file_contents():

  • returns r.text

I find that my code successfully writes .tar.gz archives most of the time, but is also prone to silent failure (e.g. occasionally writes archives that are corrupted)--for reasons that I do not understand. If you have any advice on how to download tar.gz files from Clarity, I would greatly appreciate it!

~Greg

The code:

from urllib.parse import urljoin
import tarfile

PATH_tgz = "example.tar.gz"
DIR_download = "./directory/"

make a request

r = API.request_session.get(URL, auth=(API.username, API.password), timeout=15, stream=True)
API.validate_response(r)

Write tgz and extract it

with open(PATH_tgz, 'wb') as f: f.write(r.raw.read())
FILE_tgz = tarfile.open(PATH_tgz)
FILE_tgz.extractall(DIR_download)
FILE_tgz.close()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions