Error while downloading file from sharepoint using Office365-REST-Python-Client package in python

Suryansh 1 Reputation point
2022-11-08T08:40:55.29+00:00

Hi, I am using Office365-REST-Python-Client 2.3.15 package for the first time and want to download a file from the sharepoint site.

ctx = ClientContext(<site-address>).with_credentials(client_credentials)  
  
file_url = '/sites/<site-name>/new_test/helper.xlsx'  
source_file = ctx.web.get_file_by_server_relative_path(file_url)  
  
local_file_name = os.path.basename(file_url)  
  
with open(local_file_name, "wb") as local_file:  
    source_file.download_session(local_file).execute_query()  

But it is throwing an error "File Not Found". Although when I am checking on sharepoint site the file is present there.

Error :-
office365.runtime.client_request_exception.ClientRequestException: ('-2130575338, Microsoft.SharePoint.SPException', 'The file /sites/<site-name>/new_test/helper.xlsx does not exist.', "404 Client Error: Not Found for url: https://<site-address>/_api/Web/getFileByServerRelativePath(DecodedUrl='%2Fsites%2F<site-name>%2Fnew_test%2Fhelper.xlsx')?$select=ServerRelativePath")

Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,251 Reputation points
    2022-11-09T01:56:46+00:00

    Hi @Suryansh ,

    According to my research and testing, please try to do some troubleshooting:

    • Please check what version of the Office365-REST-Python-Client package you are using. Try to install the latest version via GitHub: ( V.2.3.7 ) pip install git+https://github.com/vgrem/Office365-REST-Python-Client.git
    • Please check if the file_url is correct.

    More information for reference:

    Hope it can help you. Thanks for your understanding.

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.