使用Office365 API读取python中的.txt文件
我希望阅读使用Python存储在OneDrive中的文本文件。我有一个办公室365订阅。我使用以下代码:
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
username, password = "<username>", "<password>" #Fields replaced by placeholders
site_url = 'https://<company>-my.sharepoint.com/personal/<email_id>/'
file_url = 'Documents/textfile.txt'
ctx_auth = AuthenticationContext(site_url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(site_url, ctx_auth)
response = File.open_binary(ctx, file_url)
print(response)
info = str(response.content)
print(info)
但是运行此导致HTTP不良请求:
<Response [400]>
b'{"error":{"code":"-2147024809, System.ArgumentException","message":{"lang":"en-US","value":"serverRelativeUrl\\r\\nParameter name: Specified value is not supported for the serverRelativeUrl parameter."}}}'
在Stackoverflow上搜索,我发现file.open_binary(ctx,file_url)
必须以/<
/< /code>用于通过Python访问SharePoint文件,但是如果我使用
file_url ='/documents/textfile.txt'
for OneDrive,则会提出一个错误的请求,但会有错误消息:
<Response [400]>
b'{"error":{"code":"-2147024809, System.ArgumentException","message":{"lang":"en-US","value":"Server relative urls must start with SPWeb.ServerRelativeUrl"}}}'
但是,我可以阅读文本存储在SharePoint Hassle无需的文件:
site_url = 'https://<company>.sharepoint.com/sites/sitename/'
file_url = '/sites/sitename/Shared Documents/myfile.txt'
ctx_auth = AuthenticationContext(site_url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(file_url, ctx_auth)
response = File.open_binary(ctx, file_url)
print(response)
info = str(response.content).replace('b', '').replace('\'', '')
print(info)
输出
<Response [200]>
Hello World!
也是我在读取OneDrive作为Pandas DataFrames中存储的Excel文件时遇到的问题。
在阅读OneDrive的文本文件时,我会做错什么?
I wish to read a text file stored in OneDrive using Python. I have an Office 365 subscription. I used the following code:
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
username, password = "<username>", "<password>" #Fields replaced by placeholders
site_url = 'https://<company>-my.sharepoint.com/personal/<email_id>/'
file_url = 'Documents/textfile.txt'
ctx_auth = AuthenticationContext(site_url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(site_url, ctx_auth)
response = File.open_binary(ctx, file_url)
print(response)
info = str(response.content)
print(info)
But running this leads to an HTTP bad request:
<Response [400]>
b'{"error":{"code":"-2147024809, System.ArgumentException","message":{"lang":"en-US","value":"serverRelativeUrl\\r\\nParameter name: Specified value is not supported for the serverRelativeUrl parameter."}}}'
Searching on StackOverflow, I found that the relative URL in File.open_binary(ctx, file_url)
must begin with /
for accessing SharePoint files through Python, but if I use file_url = '/Documents/textfile.txt'
for OneDrive, a bad request is raised with an error message:
<Response [400]>
b'{"error":{"code":"-2147024809, System.ArgumentException","message":{"lang":"en-US","value":"Server relative urls must start with SPWeb.ServerRelativeUrl"}}}'
However, I can read text files stored in SharePoint hassle-free:
site_url = 'https://<company>.sharepoint.com/sites/sitename/'
file_url = '/sites/sitename/Shared Documents/myfile.txt'
ctx_auth = AuthenticationContext(site_url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(file_url, ctx_auth)
response = File.open_binary(ctx, file_url)
print(response)
info = str(response.content).replace('b', '').replace('\'', '')
print(info)
Output
<Response [200]>
Hello World!
Also, I face no problem in reading Excel files stored in OneDrive as Pandas dataframes.
What could I be doing wrong in reading text files from OneDrive?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论