Question #5783   Submitted by Answiki on 01/23/2022 at 07:05:52 PM UTC

How to access files in Google Drive from Google Colab?

Answer   Submitted by Answiki on 01/23/2022 at 07:18:41 PM UTC

To read or write files located in Google Drive from Google Colab Python scripts , the best option is to use the dedicated package from Google:

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

Once you run the above lines, Google Drive will ask for authorization. After granting access to the directory, the mounting point of the google drive should be printed in the console:

Mounted at /content/drive

To access files located in Google Drive, the path should start with /content/drive/My Drive/ as on the following example:

# Load the text file located in my_path
f = open('/content/drive/My Drive/my_path/file.txt')
# Print the text file content
print (f.read())


3 events in history
Answer by Answiki on 01/23/2022 at 07:18:41 PM

To read or write files located in Google Drive from Google Colab Python scripts , the best option is to use the dedicated package from Google:

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

Once you run the above lines, Google Drive will ask for authorization. After granting access to the directory, the mounting point of the google drive should be printed in the console:

Mounted at /content/drive

To access files located in Google Drive, the path should start with /content/drive/My Drive/ as on the following example:

# Load the text file located in my_path
f = open('/content/drive/My Drive/my_path/file.txt')
# Print the text file content
print (f.read())


Answer by Answiki on 01/23/2022 at 07:17:56 PM

To read or write files located in Google Drive from Python scripts run in Google Colab, the best option is to use the dedicated package:

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

Once you run the above lines, Google Drive will ask for authorization. After granting access to the directory, the mounting point of the google drive should be printed in the console:

Mounted at /content/drive

To access files located in Google Drive, the path should start with /content/drive/My Drive/ as on the following example:

# Load the text file located in my_path
f = open('/content/drive/My Drive/my_path/file.txt')
# Print the text file content
print (f.read())


Question by Answiki 01/23/2022 at 07:05:52 PM
How to access files in Google Drive from Google Colab?
# ID Query URL Count

Icons proudly provided by Friconix.