-
-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathexists.py
More file actions
33 lines (25 loc) · 1.16 KB
/
exists.py
File metadata and controls
33 lines (25 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
Demonstrates how to check if a file exists on a SharePoint site.
Attempts to retrieve a file by its server-relative URL and returns None if not found.
See https://learn.microsoft.com/en-us/sharepoint/dev/apis/rest-api/navigation/file-operations
"""
from http import HTTPStatus
from typing import Optional
from office365.runtime.client_request_exception import ClientRequestException
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
from office365.sharepoint.webs.web import Web
from tests import test_client_credentials, test_team_site_url
def try_get_file(web: Web, url: str) -> Optional[File]:
try:
return web.get_file_by_server_relative_url(url).select(["Exists"]).get().execute_query()
except ClientRequestException as e:
if e.response.status_code == HTTPStatus.NOT_FOUND:
return None
else:
raise ValueError(e.response.text) from e
ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
file_url = "Shared Documents/Financial Sample.xlsx"
file = try_get_file(ctx.web, file_url)
if file is None:
print("File not found")