Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Download Anything to Google Drive using Google Colab
Google Colab is a free cloud platform provided by Google that offers CPU, GPU, and other infrastructure to run complex computations and data analysis. Google Colab provides a Jupyter notebook environment to run Python code. We can download files directly to Google Drive using Google Colab by mounting our Google Drive in the Colab environment. In this article, we will learn how to download files to Google Drive using Google Colab.
Step 1: Setting up Google Colab
If you have a Google account, you can open the Google Colab website and click on "New Notebook" to create a new Jupyter Notebook. A Google account is required to access Google Colab.
Step 2: Mounting Your Google Drive
To download files to Google Drive using Google Colab, you need to mount your Google Drive account in the Colab environment. Run the following code in your Colab notebook ?
from google.colab import drive
drive.mount('/content/drive')
After running this code, you will get a prompt to authenticate your Google account and allow Colab to access your drive. Follow the instructions in the prompt and complete the authentication process.
Mounted at /content/drive
Step 3: Downloading Files to Google Drive
Once your drive is mounted to the Colab environment, you can download files from the internet directly to your Google Drive using the following syntax ?
Syntax
!wget -P /content/drive/My\ Drive [file_URL]
Here [file_URL] is the URL of the file you want to download. The !wget command is used to download files from the internet, and -P specifies the destination directory where the file will be stored in Google Drive.
Example
In the following example, we download a Python logo and save it to the root directory of our Google Drive ?
!wget -P /content/drive/My\ Drive https://www.python.org/static/img/python-logo.png
--2024-01-01 10:30:15-- https://www.python.org/static/img/python-logo.png Resolving www.python.org (www.python.org)... 151.101.112.223 Connecting to www.python.org (www.python.org)|151.101.112.223|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 8629 (8.4K) [image/png] Saving to: '/content/drive/My Drive/python-logo.png' python-logo.png 100%[===================>] 8.43K --.-KB/s in 0s 2024-01-01 10:30:15 (95.2 MB/s) - '/content/drive/My Drive/python-logo.png' saved [8629/8629]
Alternative: Downloading to Specific Folders
You can also download files to specific folders within your Google Drive by modifying the path ?
# Download to a specific folder !wget -P /content/drive/My\ Drive/Downloads https://www.python.org/static/img/python-logo.png
Verifying the Download
You can verify that the file was downloaded successfully by listing the contents of your Google Drive ?
!ls "/content/drive/My Drive"
Conclusion
Google Colab provides a convenient way to download files directly to Google Drive by mounting your drive and using the wget command. This method is particularly useful for downloading datasets, images, or any files you need for your projects while working in the cloud environment.
