You are on page 1of 2

To download a file from SharePoint using the Python `office365` library, you can

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'

client_credentials = ClientCredential(client_id, client_secret)


ctx = ClientContext(url).with_credentials(client_credentials)

# Get the target list and file


target_list = ctx.web.lists.get_by_title(list_title)
target_file = target_list.root_folder.files.get_by_name('file_to_download.txt')

# Download the file content


response = File.open_binary(ctx, target_file.serverRelativeUrl)
file_content = response.content

# Save the file content to a local file


with open('file_to_download.txt', 'wb') as local_file:
local_file.write(file_content)
```

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.

If the file you want to download is located in a subfolder,


you can get a reference to it by calling the `get_by_name()` method on the `files`
property
of the target subfolder instead of the root folder.

-------------------

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.

You can also use the `os.path.join()` function to create a platform-independent


path that works on both Windows and Linux. Here's an example:

```python
import os

# Create a platform-independent path


downloads_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
file_path = os.path.join(downloads_dir, 'file_to_download.txt')

# Save the file content to a local file


with open(file_path, 'wb') as local_file:
local_file.write(file_content)
```

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.

You might also like