我正在尝试使用 Office365-REST-Python-Client 中的示例将文件从一个文件夹移动到另一个文件夹,但它不起作用:
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 = ctx.web.get_folder_by_server_relative_url("Shared Documents")
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))
移动文件夹或删除文件进展顺利。现在移动文件的程序如何? 谢谢
可以参考以下代码
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
# Initialize client context
ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)
# Define the source file and destination folder paths
file_relative_path = "/Shared Documents/Financial Sample.xlsx"
destination_folder_relative_url = "/Shared Documents" # Update this with correct relative URL
# Get the file by its server relative path
file_from = ctx.web.get_file_by_server_relative_path(file_relative_path)
# Move the file to the destination folder using its relative URL
file_to = file_from.move_to_using_path(destination_folder_relative_url, MoveOperations.overwrite).execute_query()
# Print confirmation
print("'{0}' moved into '{1}'".format(file_relative_path, destination_folder_relative_url))