Read file from drive in google colab

前端 未结 5 1367
没有蜡笔的小新
没有蜡笔的小新 2021-01-13 06:24

I Have read the notebook about how to open drive. I already did as instructed using:

from google.colab import drive
drive.mount(\'/content/drive\')


        
相关标签:
5条回答
  • 2021-01-13 07:05

    I (partially) found out what was going on based on Bob Smith and Ami F's answers.
    I believe google drive blocks read access from files converted to drive formats (gsheet, gdoc, etc.). And so, whenever I tried to use !cat or open, I got an "operation unsupported" error. When I tried Bob's example, creating a file and then reading it, it worked on my notebook.
    So I managed to prevent google from converting files, deleted the old files and uploaded everything to drive again. Now, all my csv's were being kept unchanged (no .gsheet extesion) and I am able to access them using open.

    0 讨论(0)
  • 2021-01-13 07:07

    The fact that you see ".csv.gsheet" filenames even though you upload ".csv" filenames makes me think that you're uploading your CSVs to sheets.google.com instead of drive.google.com. Can you confirm that uploading to drive.google.com makes things work?

    0 讨论(0)
  • 2021-01-13 07:11

    It is straight forward.

    from google.colab import drive drive.mount('/content/drive')

    This will ask to open a url which will authorize the mount after you copy paste the token.

    If you are not able to read files even now, then prefix your file path with this: 'drive/My Drive' and you are good to go.

    For example: file = 'drive/My Drive/data/file.txt'

    Where data is a directory in my Google Drive containing file.txt file.

    0 讨论(0)
  • 2021-01-13 07:15

    I ran into a similar issue last night. As some of the previous responders posted there are concerns that influence your ability to read the file. These concerns are, one, making certain that your file is accessible via google drive from your Collab notebook and also, two, making certain that your file is in the correct format.

    I will explain the steps and include a screen shot.

    1. Open Google Collab. Open the File Browser.
    2. Click the icon that says Mount Drive when hovered. This inserts a new cell in your notebook with the code:
    from google.colab import drive
    drive.mount('/content/drive')
    

    Run the cell. You are prompted to accept permissions and get a token to use to mount the drive. Grant the permissions and copy and paste the code into the text input. Hit enter.

    1. The drive now appears in the file browser. Right click the folder /drive/My Drive or click the three dots action menu and select Upload.
    2. Locate your file on disk and Upload.
    3. The file appears in the File Browser. Right click the File (or use the three dots action menu) and select Copy Path.
    4. Paste that file path into your pd.read_csv() call.
    5. Run the cell with the pd.read_csv function call.
    6. You should now have the file uploaded in your Google Drive. Accessible to google collab and file formatting preserved because it not been accessed by any other program to munge the format.

    Below is the example sans Permission tab because I previously granted permissions.

    0 讨论(0)
  • 2021-01-13 07:23

    I just tried mounting and creating a Drive file as you described and couldn't reproduce the error you describe.

    https://colab.research.google.com/drive/17iiKJPQOPv1eW5-Ctf707mPHXDtipE5G

    Perhaps try resetting your backend using the Runtime -> Reset all runtimes menu. Or, can you share a notebook illustrating the problem?

    0 讨论(0)
提交回复
热议问题