It’s 2023, stop leaking secrets in google colab! Part 1

Maxwell Langford
3 min readApr 2, 2023

--

Photo by Michael Dziedzic on Unsplash

Google Colab (short for Collaboratory) is a cloud-based platform that allows users to write and run Python code using Jupyter Notebook-style notebooks. Colab notebooks are stored in Google Drive and can be shared with others for collaboration. Here are some key features of Google Colab:

  1. Free GPU and TPU access: Google Colab provides free access to GPU (Graphics Processing Unit) and TPU (Tensor Processing Unit) resources, which can greatly accelerate machine learning tasks and other computationally-intensive workloads.
  2. Built-in libraries: Google Colab comes with a variety of pre-installed Python libraries and supports popular data science and machine learning frameworks such as TensorFlow, PyTorch, and scikit-learn.
  3. Integration with Google services: Google Colab integrates with other Google services such as Google Drive, Google Sheets, and Google BigQuery, making it easy to import and export data.
  4. Interactive notebooks: Colab notebooks are interactive, allowing users to write and run code, create visualizations, and add explanatory text and images in the same document.
  5. Collaboration: Google Colab allows multiple users to collaborate on the same notebook in real-time, making it a useful tool for team projects and code reviews.

Google Colab can be used for a variety of tasks, including data analysis, machine learning, deep learning, natural language processing, and more. It is especially useful for those who do not have access to powerful computing resources, as it allows them to take advantage of Google’s cloud computing infrastructure for free. Additionally, it can be a useful tool for educational purposes, as it provides an interactive environment for learning and experimenting with Python and machine learning concepts.

from google.colab import files
import os

# Check if file already exists
if os.path.exists('.env'):
os.remove('.env')

# Upload file
uploaded = files.upload()
file_name = list(uploaded.keys())[0]

# Rename file
try:
os.rename(file_name, '.env')
print('File uploaded and renamed successfully.')
except:
print('Error renaming file.')

Here is an explanation of the code, so you don’t just blindly copy paste this:

  1. from google.colab import files: This imports the files module from the google.colab library, which provides functionality for uploading and downloading files in Google Colab.
  2. import os: This imports the os module, which provides a way to interact with the operating system and perform operations like file deletion and renaming.
  3. if os.path.exists('.env'): os.remove('.env'): This checks if a file with the name .env already exists in the current working directory. If it does, it is removed using the os.remove() method. This is done to ensure that the file being uploaded and renamed doesn't conflict with an existing file.
  4. uploaded = files.upload(): This prompts the user to upload a file from their local computer to the current working directory in Google Colab.
  5. file_name = list(uploaded.keys())[0]: This extracts the name of the uploaded file from the dictionary returned by files.upload(). list(uploaded.keys()) gives a list of keys in the dictionary (which are the file names uploaded), and [0] takes the first one (in case multiple files were uploaded).
  6. try: os.rename(file_name, '.env') and except: print('Error renaming file.'): This attempts to rename the uploaded file to .env using os.rename(). If the rename operation is successful, a message indicating success is printed to the console. If an error occurs during the rename operation, a message indicating the error is printed instead.

This code checks for and removes an existing .env file (if it exists), prompts the user to upload a new file, and attempts to rename the uploaded file to .env. It also provides some basic error handling in case something goes wrong during the renaming operation.

Part two will walk you through calling environemntal varaiables from these files!

--

--

Maxwell Langford

Maxwell Langford, 40s, veteran computer scientist. Expert in Python, Java, C++, JavaScript. Educator and mentor. Enjoys exploring new topics in CS & technology.