How to Use Google Colab to Work with Local Files
What is Google Colab?
Google Colab, or Colaboratory, is a free cloud-based platform that allows developers to write and run code in a Jupyter notebook environment. It offers free access to GPU and TPU, which are essential for machine learning and deep learning algorithms. Google Colab also provides free storage and easy sharing options for notebooks.
Uploading Files to Google Colab
Before we can access local files on Google Colab, we need to upload them to the Colab environment. Here are the steps to do so:
- Open Google Colab and create a new notebook.
- Click on the “Files” tab on the left-side menu. (Make sure it is the “Files tab” not the “File” Dropdown menu)
- Click on the “Upload to Session Storage” button and select the file(s) you want to upload.
- Wait for the upload to complete.
Once the files are uploaded, you can access them in the “Files” tab.
Mounting Google Drive
Another way to access local files on Google Colab is by mounting your Google Drive. This is useful if you have a large number of files or a folder structure that you want to work with.
Here are the steps to mount your Google Drive:
- Open Google Colab and create a new notebook.
- Run the following code:
from google.colab import drive
drive.mount('/content/drive')
- Follow the link generated by the code and sign in to your Google account.
- Copy the verification code and paste it into the input box in the notebook.
Once your Google Drive is mounted, you can access your files and folders in the “/content/drive/MyDrive/” directory.
Accessing Local Files
Now that we have uploaded our files and mounted our Google Drive, let’s discuss how to access local files on Google Colab.
Reading Local Files
To read a local file, we can use the built-in Python “open” function. Here is an example:
with open('file.txt', 'r') as f:
print(f.read())
In this example, we open the file “file.txt” in read mode and print its contents.
Writing Local Files
To write to a local file, we can use the built-in Python “open” function with the “w” mode. Here is an example:
with open('file.txt', 'w') as f:
f.write('Hello, World!')
In this example, we open the file “file.txt” in write mode and write the string “Hello, World!” to it.
Copying Local Files
To copy a local file, we can use the built-in Python “shutil” module. Here is an example:
import shutil
shutil.copy2('file.txt', 'file_copy.txt')
In this example, we copy the file “file.txt” to “file_copy.txt”.
Conclusion
In this tutorial, we discussed how to access and work with local files on Google Colab. We learned how to upload files, mount Google Drive, and read, write, and copy local files. As a software engineer, having the ability to work with local files on Google Colab can be a valuable tool for your development projects.
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Request a demo today to learn more.
Saturn Cloud provides customizable, ready-to-use cloud environments for collaborative data teams.
Try Saturn Cloud and join thousands of users moving to the cloud without
having to switch tools.