bug (resolved) with “Mount drive” web-button in colab. Accessing “shared with me” files from google colab (y2020, previous solutions seem to fail)

前端 未结 2 1202
隐瞒了意图╮
隐瞒了意图╮ 2021-01-23 06:45

[ Newer Edit]: colab team reported that they corrected the issue on May 27 2020. I have checked - it works Okay for me now.
Link to issue: https://github.com/googlecolab/col

2条回答
  •  温柔的废话
    2021-01-23 06:58

    Suppose you want to read a shared csv file from drive. You have done "Add shortcut to Drive".

    1) At Colab Notebook Connect to your drive.

    # Import PyDrive and associated libraries.
    # This only needs to be done once per notebook.
    from pydrive.auth import GoogleAuth
    from pydrive.drive import GoogleDrive
    from google.colab import auth
    from oauth2client.client import GoogleCredentials
    
    # Authenticate and create the PyDrive client.
    # This only needs to be done once per notebook.
    auth.authenticate_user()
    gauth = GoogleAuth()
    gauth.credentials = GoogleCredentials.get_application_default()
    drive = GoogleDrive(gauth)
    

    2) Get the id of shared file you want to access. Open file -> go to linksharing [https://drive.google.com/open?id=1JKECh3GNry6xbAK6aBSzQtSntD4GTEl ] -> copy the the string after 'id='

    3) back to colab

    # A file ID looks like: laggVyWshwcyP6kEI-y_W3P8D26sz
    file_id = '1JKECh3GNry6xbAK6aBSzQtSntMD4GTEl'
    
    downloaded = drive.CreateFile({'id': file_id}) #important
    print(downloaded['title'])  # it should print the title of desired file
    downloaded.GetContentFile('file.csv')  
    #Finally, you can read the file as pandas dataframe. 
    import pandas as pd
    df= pd.read_csv('file.csv') 
    

    Note : This is my first ever answer to a stack overflow question

提交回复
热议问题