こんにちは@vgrem 、次のコードはValueError
ます。
...
web = client_context.web
client_context.load(web)
server_relative_url = f'/{folder_name}/{filename}'
logger.info(f'relative path: {server_relative_url}')
file = web.get_file_by_server_relative_url(server_relative_url)
return file.read()
このようなエラーにメッセージを追加するのは問題ありません
`` `
ValueErrorを発生させます( 'あなたはこれとそれを間違ってしました...')
file.read()
前にこれを追加すると、問題が修正されました
client_context.load(file)
client_context.execute_query()
そうです、これらは欠落しているメソッドFile.read
メソッドは、ファイルがダウンロードされる前に、ファイルメタデータ(特にファイルを適切にアドレス指定するためのServerRelativeUrl
プロパティ)が取得されることを期待しています。
client_context.load(file)
client_context.execute_query()
ファイルの内容をダウンロードするのはそれほど直感的ではないかもしれないので、別の方法File.download
が導入され、ファイルは次のようにダウンロードできるようになりました。
with open(download_path, "wb") as local_file:
source_file = ctx.web.get_file_by_server_relative_url(file_url)
source_file.download(local_file)
ctx.execute_query()
@vgremに感謝し初心者にとって使いやすさが大幅に向上します。
file.downloadは2.1.7.post1の一部ですか?
ピップからの最新のものはそこまでしか行けません
ピップリスト| grep-iオフィス
Office365-REST-Python-クライアント2.1.7.post1
2.1.8
pip install --upgrade Office365-REST-Python-Client == 2.1.8
エラー:Office365-REST-Python-Client == 2.1.8の要件を満たすバージョンが見つかりませんでした(バージョン:1.0.0、1.0.1、1.1.0、2.0.0、2.1.1、2.1.2から) 、2.1.3、2.1.4、2.1.5、2.1.6.post2、2.1.7.post1)
エラー:Office365-REST-Python-Client == 2.1.8に一致するディストリビューションが見つかりません
====一時的な回避策====
一時ファイルをインポートする
OSのインポート
download_path = os.path.join(tempfile.mkdtemp()、os.path.basename(location))
file_url = '/ sites / Integration / Shared Documents / SomeFolder / somefile.csv'
download_path = os.path.join(tempfile.mkdtemp()、os.path.basename(file_url))
download_path
open(download_path、 "wb")をlocal_fileとして使用:
source_file = ctx.web.get_file_by_server_relative_url(file_url)
ソースファイル
ctx.load(source_file)
ctx.execute_query()
r = source_file.read()
ctx.execute_query()
print( "データ:"、r)
local_file.write(r)
local_file.close()
print( "[OK]ファイルがダウンロードされました:{0}"。format(download_path))