首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Python将文件从一个文件夹移动到sharepoint中的另一个文件夹

使用Python将文件从一个文件夹移动到sharepoint中的另一个文件夹
EN

Stack Overflow用户
提问于 2022-01-24 14:23:52
回答 1查看 913关注 0票数 1

我试图使用库SharePoint将文件从一个文件夹移动到另一个文件夹。

下面是我写的代码-

代码语言:javascript
运行
复制
from office365.runtime.auth.client_credential import ClientCredential
from office365.runtime.client_request_exception import ClientRequestException
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.files.file import File

ctx_auth = AuthenticationContext(url_shrpt)
if ctx_auth.acquire_token_for_user(username_shrpt,password_shrpt):
  ctx = ClientContext(url_shrpt, ctx_auth)
  file = ctx.web.get_file_by_server_relative_url(old_url)
  ctx.load(file)
  ctx.execute_query()
  File.moveto(ctx, new_relative_url=new_url, flag=1)

我搞错了-

代码语言:javascript
运行
复制
"'ClientContext' object has no attribute 'context'"
AttributeError                            Traceback (most recent call last)
<command-527297> in <module>
      6   ctx.load(file)
      7   ctx.execute_query()
----> 8   File.copyto(ctx, new_relative_url=new_url, overwrite=True)

/databricks/python/lib/python3.8/site-packages/office365/sharepoint/files/file.py in copyto(self, new_relative_url, overwrite)
    124         :type overwrite: bool
    125         """
--> 126         qry = ServiceOperationQuery(self,
    127                                     "CopyTo",
    128                                     {

/databricks/python/lib/python3.8/site-packages/office365/runtime/queries/service_operation_query.py in __init__(self, binding_type, method_name, method_params, parameter_type, parameter_name, return_type, is_static)
     16         :type method_name: str or None
     17         """
---> 18         super(ServiceOperationQuery, self).__init__(binding_type.context,
     19                                                     binding_type,
     20                                                     parameter_type,

AttributeError: 'ClientContext' object has no attribute 'context'

有人能帮我解决这个错误吗?

EN

回答 1

Stack Overflow用户

发布于 2022-08-30 08:26:22

也许你可以试试这个:

代码语言:javascript
运行
复制
from office365.runtime.auth.client_credential import ClientCredential
from office365.runtime.client_request_exception import ClientRequestException
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.files.file import File

ctx_auth = AuthenticationContext(url_shrpt)
if ctx_auth.acquire_token_for_user(username_shrpt,password_shrpt):
  ctx = ClientContext(url_shrpt, ctx_auth)
  source_file = ctx.web.get_file_by_server_relative_url(old_url)
  source_file.moveto(new_relative_url=new_url, flag=1)
  ctx.execute_query()

谢谢。

参考资料:https://github.com/vgrem/Office365-REST-Python-Client/issues/157

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70835375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档