Skip to content

Instantly share code, notes, and snippets.

@s2kw
Last active September 30, 2022 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s2kw/995a26a1b3f463cc7430b1c205f7a171 to your computer and use it in GitHub Desktop.
Save s2kw/995a26a1b3f463cc7430b1c205f7a171 to your computer and use it in GitHub Desktop.
Google Drive上のテキストファイルを読む
# ドライブ上のテキストファイルの中身を返す
def readTextOnDrive(gdrive, file_name) -> str:
# 条件を与えて一覧(複数あれば)を取得
fileList = gdrive.ListFile({'q': 'title = "{}"'.format(file_name)}).GetList()
# 最初の1つ目のIDを色々使う
id = fileList[0]['id']
# 何するにでもCreateFileをしなけりゃならない。CreateFileだけならカラのファイル
f = gdrive.CreateFile({'id': id})
# ファイルを作業領域に作成する。以降、同ファイル名でローカルにアクセスできる。
f.GetContentFile( filename = "任意のファイル名" )
# readで読んだバイトデータをdecodeで変換
text=f.content.read().decode( 'utf-8' )
return text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment