Accessing Files on Colab
There are two main methods to access data for Google Colab. First, you can upload a single file and work with it. Second, you can mount your Google Drive, and then you could access files similar to how you access files on your computer. This is best when you have multiple files.
1. Uploading a Single File
Another option is to directly upload the files from your local machine into Google Colab. To do this, we import the files library from google.colab and use the upload function.
from google.colab import files
uploaded = files.upload()
After you run the lines above, you should see the prompt.

Navigate to the file to choose that file to upload. Save all your course files to a single folder to make it easier to access later.
2. Mounting a Google Drive Folder
Second, let's connect your Google Drive to the Google Colab so you can upload files from your Google Drive folder to use. To do this, we import a library from google.colab and use the drive.mount function.
from google.colab import drive
drive.mount('/content/drive')
When you mount your Google Drive by running the lines above, you should receive a prompt to click on a link. Click the link and follow the instructions to allow Google Colab to access your Google drive. Make sure that it's the email address linked to the Google Drive that you plan to use for Colab.
Once you click the link and give Google Colab those permissions, you'll receive an authorization code. Input that authorization code into the prompt space.

Working Directory
The working directory is where the code will read and write files from. Check to see where your current working directory is using the !pwd
command. The "!" indicates that this is a command line function, and "pwd" returns the current working directory.
!pwd
Changing the Working Directory
To change the working directory, you'll need to use the os package and the change directory function.
import os
os.chdir('/content/drive/My Drive/Colab Notebooks/data')
Additional Reading
For more information about syntax, review the Python Data Science Handbook, chapter 1 section 5.
Last updated