我正在尝试下载 4 个(或更多)子文件夹内的所有文件,这些子文件夹位于 SharePoint 中名为
'Reports'
的主文件夹内。
我使用了以下代码:
url = 'https://HAHA.sharepoint.com/sites/Forms/Reports/'
import getpass
domain = 'HAHA.sharepoint.com'
user = getpass.getuser()
pwd = getpass.getpass(prompt='What is your windows AD password?')
import requests
from requests_ntlm import HttpNtlmAuth
from urllib.parse import unquote
from pathlib import Path
filename = unquote(Path(url).name)
response = requests.get(url, auth=HttpNtlmAuth(f'{domain}\\{user}', pwd ))
for file in filename:
with open(filename, 'wb') as local_file:
local_file.write(response.content)
但是,我收到错误:
OSError:[Errno 22]无效参数:'AllItems.aspx?FolderCTID=0x0120001B8F714072E1184781C4A5E4969E4CC9&id=/Reports'
有人能帮我解决这个问题吗?我想知道这段代码是否专门下载一个文件。我想下载整个文件夹及其子文件夹以及子文件夹中的文件。
您可以参考以下代码获取子文件夹中的文件
import os
import sharepy
s = sharepy.connect("https://example.sharepoint.com")
site = "https://example.sharepoint.com/sites/test"
library = "Test Library"
# Get list of all files and folders in library
files = s.get("{}/_api/web/lists/GetByTitle('{}')/items?$select=FileLeafRef,FileRef"
.format(site, library)).json()["d"]["results"]
for file in files:
source = "https://" + s.site + file["FileRef"]
dest = "." + file["FileRef"]
# Check if item is file or folder
folder = s.get("{}/_api/web/GetFolderByServerRelativeUrl('{}')"
.format(site, file["FileRef"])).status_code == 200
print(source)
if not os.path.exists(dest):
if folder:
os.makedirs(dest) # Create folders
else:
s.getfile(source, filename=dest) # Download files