`
Код: Выделить всё
ctx_auth = AuthenticationContext(site_url)
пароль):
Код: Выделить всё
ctx = ClientContext(site_url, ctx_auth)
print("Successfully authenticated")
def download_files(folder_url, local_path):
"""Funkcja do pobierania plików z SharePointa."""
folder = ctx.web.get_folder_by_server_relative_url(folder_url)
ctx.load(folder)
ctx.execute_query()
files = folder.files
ctx.load(files)
ctx.execute_query()
if not os.path.exists(local_path):
os.makedirs(local_path)
for file in files:
# Pobierz plik i zapisz go lokalnie
file_name = file.properties["Name"]
file_path = os.path.join(local_path, file_name)
with open(file_path, "wb") as local_file:
file_content = File.open_binary(ctx, file.properties["ServerRelativeUrl"]).content
local_file.write(file_content)
print(f"Pobrano plik: {file_name}")
def download_contents(folder_url, local_path):
"""Funkcja do rekursywnego pobierania zawartości folderów z SharePointa."""
folder = ctx.web.get_folder_by_server_relative_url(folder_url)
ctx.load(folder)
ctx.execute_query()
folders = folder.folders
files = folder.files
ctx.load(folders)
ctx.load(files)
ctx.execute_query()
if not os.path.exists(local_path):
os.makedirs(local_path)
for subfolder in folders:
# Rekurencyjnie pobierz zawartość folderu
encoded_subfolder_url = urllib.parse.quote(subfolder.properties["ServerRelativeUrl"], safe=':/')
download_contents(encoded_subfolder_url, os.path.join(local_path, subfolder.properties["Name"]))
for file in files:
# Pobierz plik i zapisz go lokalnie
file_name = file.properties["Name"]
file_path = os.path.join(local_path, file_name)
with open(file_path, "wb") as local_file:
file_content = File.open_binary(ctx, file.properties["ServerRelativeUrl"]).content
local_file.write(file_content)
print(f"Pobrano plik: {file_name}")
# Wywołanie funkcji do pobierania plików i zawartości folderów z SharePointa
download_files("/Shared Documents", local_folder_path)
download_contents("/Shared Documents", local_folder_path)
Код: Выделить всё
print(ctx_auth.get_last_error(
`
Я из Польши, поэтому, если вам нужны какие-то полированные слова, просто игнорируйте их
Пример папки — D'ARMOR, а файл внутри, например, будет just_file.exe.
И на консоли отображается сообщение об ошибке 400, запрос кровати. для getFolderByServerRelativePath
Проблема заключается в апострофе между D и A, который обрезает ссылку на файл посередине
Подробнее здесь: https://stackoverflow.com/questions/787 ... -in-folder