You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Office365-REST-Python-Client读取SharePoint文件时遇文件不存在错误

问题排查与解决方案

核心问题:站点URL与文件相对路径不匹配

你当前把site_url设为根站点https://mycompany.sharepoint.com,但文件实际在/sites/mysite子站点下,ClientContext需要指向子站点的完整URL,而非根站点。同时,文件的相对路径要基于子站点,不能包含/sites/mysite前缀。

修正后的代码

from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File

# 改为子站点的完整URL
site_url = "https://mycompany.sharepoint.com/sites/mysite"
# 相对路径基于子站点,仅保留Shared Documents及后续部分
relative_url = "/Shared%20Documents/sampledata.xlsx"

ctx = ClientContext(site_url).with_credentials(UserCredential("username", "password"))
response = File.open_binary(ctx, relative_url)

# 可选:将文件保存到本地验证
with open("sampledata.xlsx", "wb") as f:
    f.write(response.content)

额外验证要点

  • 路径编码可以简化:直接用/Shared Documents/sampledata.xlsx(带空格的库名),客户端会自动处理URL编码
  • 确认文件确实存在于该子站点的Shared Documents库中,注意检查子文件夹层级(如果有的话,需补充完整路径,比如/Shared Documents/Reports/sampledata.xlsx)
  • 部分环境可能对路径大小写敏感,尽量和SharePoint页面显示的路径保持一致

内容的提问来源于stack exchange,提问作者Gattaca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 23:50:33