Office365-REST-Python-Client icon indicating copy to clipboard operation
Office365-REST-Python-Client copied to clipboard

Error on Sharepoint File download

Open vchalmel opened this issue 3 weeks ago • 1 comments

Hello,

I'm unable to download files from Sharepoint following the examples provided in the documentation, and the error I receives leaves me puzzled

I'm able to connect / browse and retrieve files metadata.

with f an instance of office365-rest-python-client/office365/sharepoint/files/file.py File, retrieved as an element of folder.get_files().get().execute_query()

I tried both

    with open(os.path.join('.', f.name), 'wb') as local_file:
        ctx.web.get_file_by_id(f.unique_id).execute_query().download(local_file)

and

    with open(os.path.join('.', f.name), 'wb') as local_file:
        f.download(local_file).execute_query()

Both thoses attempts will fail with the same issue

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[42], line 6
      4 try:
      5     with open(os.path.join('.', f.name), 'wb') as local_file:
----> 6         ctx.web.get_file_by_id(f.unique_id).execute_query().download(local_file)
      7 except:
      8     raise

File .venv/lib/python3.12/site-packages/office365/runtime/client_object.py:55, in ClientObject.execute_query(self)
     52 def execute_query(self):
     53     # type: () -> Self
     54     """Submit request(s) to the server."""
---> 55     self.context.execute_query()
     56     return self

File .venv/lib/python3.12/site-packages/office365/runtime/client_runtime_context.py:173, in ClientRuntimeContext.execute_query(self)
    171 while self.has_pending_request:
    172     qry = self._get_next_query()
--> 173     self.pending_request().execute_query(qry)
    174 return self

File .venv/lib/python3.12/site-packages/office365/runtime/client_request.py:39, in ClientRequest.execute_query(self, query)
     37     response = self.execute_request_direct(request)
     38     self.process_response(response, query)
---> 39     self.afterExecute.notify(response)
     40 except HTTPError as e:
     41     raise ClientRequestException(*e.args, response=e.response)

File .venv/lib/python3.12/site-packages/office365/runtime/types/event_handler.py:41, in EventHandler.notify(self, *args, **kwargs)
     39 if self._once:
     40     self._listeners.remove(listener)
---> 41 listener(*args, **kwargs)

File .venv/lib/python3.12/site-packages/office365/runtime/client_runtime_context.py:141, in ClientRuntimeContext.after_query_execute.<locals>._process_response(resp)
    139 if self.current_query.id == query.id:
    140     self.pending_request().afterExecute -= _process_response
--> 141     action(resp if include_response else query.return_type)

File .venv/lib/python3.12/site-packages/office365/sharepoint/files/file.py:662, in File.download.<locals>._save_content(return_type)
    660 def _save_content(return_type):
    661     # type: (ClientResult[AnyStr]) -> None
--> 662     file_object.write(return_type.value)
    663     if callable(after_downloaded):
    664         after_downloaded(self)

ValueError: write to closed file

I don't understand how my local file can be closed as we only go deeper and never exit the with statement

vchalmel avatar Dec 01 '25 12:12 vchalmel

ctx.web.get_file_by_id(f.unique_id).download(local_file).execute_query()

Is working tho but I would really be interested by an explanation highlighting why those are different

vchalmel avatar Dec 01 '25 14:12 vchalmel