はじめまして。python初学者です。
PythonでGoogle sheetの読み書きをしたく、もろもろの設定をしたのちコードを実行するとJsonファイルが見つからないとエラーが出てしまいます。
自分で検索しても解決できず、解決方法を教えていただけないでしょうか。
PythonファイルとJSONは同じフォルダに入っています。
python
1import gspread 2import json 3 4#ServiceAccountCredentials:Googleの各サービスへアクセスできるservice変数を生成します。 5from oauth2client.service_account import ServiceAccountCredentials 6 7#2つのAPIを記述しないとリフレッシュトークンを3600秒毎に発行し続けなければならない 8scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive'] 9 10#認証情報設定 11#ダウンロードしたjsonファイル名をクレデンシャル変数に設定(秘密鍵、Pythonファイルから読み込みしやすい位置に置く) 12credentials = ServiceAccountCredentials.from_json_keyfile_name('XXXXXXXXXX.json', scope) 13 14#OAuth2の資格情報を使用してGoogle APIにログインします。 15gc = gspread.authorize(credentials)
Errorメッセージ
Errorメッセージ
1 File "/Volumes/GoogleDrive/My Drive/YYYY/local/70. Mics/python /AAAAAAA.py", line 12, in <module> 2 credentials = ServiceAccountCredentials.from_json_keyfile_name('XXXXXXXXXX.json', scope) 3 File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/oauth2client/service_account.py", line 219, in from_json_keyfile_name 4 with open(filename, 'r') as file_obj: 5FileNotFoundError: [Errno 2] No such file or directory: 'XXXXXXXXXX.json'
カレントディレクトリはファイルの場所ですか?
Path.cwd()
で確認してみてください。Path は pathlib モジュールにあります。
回答1件
あなたの回答
tips
プレビュー