DEV Community

Cover image for Access Google Drive Files in Google Colab
PDF Python Hub
PDF Python Hub

Posted on Edited on Originally published at payhip.com

Access Google Drive Files in Google Colab

This mini-guide explains how to connect Google Drive to Google Colab and access your PDF file using Python.

1. Upload Your PDF File to Google Drive

Upload the PDF file you want to use to your Google Drive.

2. Mount Google Drive

Run the following code in your Colab notebook:

from google.colab import drive

drive.mount("/content/drive")
Enter fullscreen mode Exit fullscreen mode

Follow the prompts to authorize access to your Google Drive.

Once mounted, your file will be accessible through the /content/drive directory.

3. Specify the File Path

Define the path to your PDF file:

file_path = "/content/drive/MyDrive/data.pdf"
Enter fullscreen mode Exit fullscreen mode

Replace data.pdf with your actual filename.

If the PDF file is in a subfolder, include the subfolder name, for example Projects, in the path:

file_path = "/content/drive/MyDrive//Projects/data.pdf"
Enter fullscreen mode Exit fullscreen mode

To check whether the file exists at file_path in the Google Colab environment, run:

import os

print(os.path.exists(file_path))
Enter fullscreen mode Exit fullscreen mode

Complete Example

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

file_path = "/content/drive/MyDrive/data.pdf"

import os
print(os.path.exists(file_path))
Enter fullscreen mode Exit fullscreen mode

Open the notebook

Open the Google Colab notebook for this PDF mini-guide and run the code as you follow along.

[Open in Google Colab]


This mini-guide was originally published on PDF Python Hub, where you can find more in-depth Python resources for working with PDFs.


What's Next? Install PyMuPDF in Google Colab

Learn how to install PyMuPDF so you can start working with PDF documents in Google Colab.

How to Install PyMuPDF in Google Colab →

Top comments (0)