0

Is there a way to stream a file directly to the filesystem? Even if the connection is lost, I want to see all contents in that specific file in the fs. Just like wget or curl.

However, using request leads to the issue of first downloading the content of a response and then writing it to a filesystem.

with open(file_name, "wb") as file:
    response = get(url) # may take some time
    file.write(response.content)

Problem: while the file is "downloading" it is stored elsewhere (I guess in memory or a temporarily splace in the filesystem). That means I have a 0 byte file as long as the request is not (successfully) finished.

Can I solve this problem without using a third party lib?

DeVolt
  • 321
  • 1
  • 3
  • 15

1 Answers1

1

Streaming directly to file can be achieved with requests and stream=true, or see more useful examples

with open(file_name, 'wb') as f:
    with requests.get(url, stream=True) as r:
        shutil.copyfileobj(r.raw, f)
makozaki
  • 3,772
  • 4
  • 23
  • 47