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

Cannot download files with an apostrophe < ' > in path using file.read()

Open le-codeur-rapide opened this issue 1 year ago • 2 comments

Hi I was using the library to download some files from sharepoint using file.read()

` for _, row in tqdm.tqdm(df.iterrows()): file_url = row['Relative URL'] local_path = os.path.join(local_folder, row['Relative URL'].lstrip('/'))

    # Create the local directory if it doesn't exist
    os.makedirs(os.path.dirname(local_path), exist_ok=True)

    # Get the file from SharePoint
    file = ctx.web.get_file_by_server_relative_path(str(file_url))
    ctx.load(file)
    ctx.execute_query()

    # Save the file locally
    with open(local_path, 'wb') as local_file:
        file.read()  

`

I had issues with files that had an appostrophe ex: < macdonald's.pdf > because the "'" is not encoded in the final url

Replacing file.read() by file.download(local_file).execute_query() It is working fine.

Is it expected behaviour ? is file.read() not supposed to handle the appostrophe also ?

Thank you for your answer !

le-codeur-rapide avatar Aug 06 '24 17:08 le-codeur-rapide

Faced this issue too - see my question #918

I fixed by encoding the relative URL myself.

CTPassion avatar Dec 05 '24 15:12 CTPassion

The PR addressing this has been merged.

CTPassion avatar Dec 06 '24 09:12 CTPassion