Office365-REST-Python-Client调用move_to_using_path报错的修正方法
问题:Office365-REST-Python-Client 文件移动报错:
File does not have a move_to_using_path function defined. 我用Python 3.10学习使用Office365-REST-Python-Client操作SharePoint文件,要实现文件移动功能,参考官方示例编写的代码如下:
from office365.sharepoint.client_context import ClientContext from office365.sharepoint.files.move_operations import MoveOperations from tests import test_team_site_url, test_user_credentials ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials) file_from = ctx.web.get_file_by_server_relative_path( "Shared Documents/Financial Sample.xlsx" ) folder_to = "Shared Documents" file_to = file_from.move_to_using_path( folder_to, MoveOperations.overwrite ).execute_query() print("'{0}' moved into '{1}'".format(file_from, file_to))
运行代码时触发错误:
File does not have a move_to_using_path function defined.
解决方案
1. 升级库到最新版本
move_to_using_path是Office365-REST-Python-Client较新版本新增的方法,旧版本无此接口。执行以下命令升级:
pip install --upgrade office365-rest-python-client
2. 改用旧版本兼容的move_to方法(无法升级时备选)
如果暂时无法升级库,可替换为旧版本支持的move_to方法,注意目标路径需包含完整文件名,而非仅文件夹路径:
from office365.sharepoint.client_context import ClientContext from tests import test_team_site_url, test_user_credentials ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials) file_from = ctx.web.get_file_by_server_relative_path( "Shared Documents/Financial Sample.xlsx" ) # 目标路径需包含文件名,示例为移动到同文件夹并改名 target_file_path = "Shared Documents/Financial Sample_Moved.xlsx" file_to = file_from.move_to(target_file_path, overwrite=True).execute_query() print(f"'{file_from.serverRelativeUrl}' moved into '{file_to.serverRelativeUrl}'")
3. 验证方法是否存在
升级后可通过以下代码确认move_to_using_path方法已存在:
print(dir(file_from))
若输出结果包含move_to_using_path,则版本更新成功。
内容的提问来源于stack exchange,提问作者reza
相关产品推荐
相关产品推荐

