Professional Documents
Culture Documents
use the `File.open_binary()` method to get the content of the file as a stream of
bytes. Here's an example that shows how to download a file named
`file_to_download.txt` from the root folder of a SharePoint list:
```python
from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.client_context import ClientContext
url = 'https://sagemcom365.sharepoint.com/sites/DSIReferentiels'
client_id = 'b7fd9c81-a58d-4f81-9e3f-ee7da4080310'
client_secret = 'HIAVPRU1AwCg0rvMmJCgoI2vdNCemlCC7YeC/CN+aHc='
list_title = 'TestReferentiel'
This code downloads the content of the file `file_to_download.txt` from the root
folder of the target SharePoint list
and saves it to a local file with the same name.
You can change the filename and path of the local file to match your needs.
-------------------
To specify the directory where the downloaded file will be saved, you can provide
the full path to the file when opening it for writing. For example,
if you want to save the downloaded file to the `C:\Users\YourName\Downloads`
directory on Windows, you can do it like this:
```python
# Save the file content to a local file
with open(r'C:\Users\YourName\Downloads\file_to_download.txt', 'wb') as local_file:
local_file.write(file_content)
```
Make sure to use the correct path separator for your operating system (`\` for
Windows and `/` for macOS and Linux). You can also use the `os.path.join()`
function to create a platform-independent path.
This code will save the downloaded file to the specified directory with the name
`file_to_download.txt`.
You can change the filename and path to match your needs.
The code I provided earlier for downloading a file from SharePoint and saving it to
a local directory should work on Linux without any changes, except for the file
path.
On Linux, the path separator is `/` instead of `\` as on Windows. So, if you want
to specify the directory where the downloaded file will be saved on a Linux system,
you need to use the correct path separator. For example, if you want to save the
downloaded file to the `/home/yourname/Downloads` directory on Linux, you can do it
like this:
```python
# Save the file content to a local file
with open('/home/yourname/Downloads/file_to_download.txt', 'wb') as local_file:
local_file.write(file_content)
```
This code will save the downloaded file to the specified directory with the name
`file_to_download.txt`. You can change the filename and path to match your needs.
```python
import os
This code uses `os.path.expanduser('~')` to get the path to the user's home
directory
and `os.path.join()` to create a path to the `Downloads` directory and the target
file.
This path will work on both Windows and Linux.