DRIVE MOUNT
drive.flush_and_unmount()
from google.colab import drive
drive.mount('/content/drive')
DRIVE UPLOAD
import os
# Replace this with your URL
url = "https://example.com/path/to/your/file.zip"
# Choose destination path in your Google Drive
# Example: saving into "MyDrive/Downloads" folder
output_path = "/content/drive/MyDrive/Downloads/file.zip"
# Make sure destination folder exists
os.makedirs(os.path.dirname(output_path), exist_ok=True)
# Download the file using wget
!wget -O "{output_path}" "{url}"
ZIP TO UNZIP
!unzip "/content/drive/MyDrive/Downloads/file.zip" -d "/content/drive/MyDrive/Downloads/"
!apt-get install unrar -y
!unrar x "/content/drive/MyDrive/Downloads/file.rar" "/content/drive/MyDrive/Downloads/"
To extract files from a remote URL in Google Colab and directly save them to your Google Drive, you can follow this simple step-by-step method.
✅ STEP-BY-STEP GUIDE
1. Mount Google Drive
from google.colab import drive
drive.mount('/content/drive')
This gives Colab access to your Google Drive. After running, you'll get a link to authorize access. Paste the code it gives you.
2. Set Destination Folder in Drive
Set the path where you want to save the extracted files (you can change this):
import os
drive_folder = '/content/drive/MyDrive/ExtractedFiles'
os.makedirs(drive_folder, exist_ok=True)
3. Download File from Remote URL
Let's assume you want to download a zip file from a remote URL:
import requests
# URL of the remote file (e.g., zip)
url = 'https://example.com/somefile.zip' # Replace this with your file URL
filename = url.split('/')[-1]
local_path = os.path.join('/content', filename)
# Download the file
response = requests.get(url)
with open(local_path, 'wb') as f:
f.write(response.content)
print(f"Downloaded to {local_path}")
4. Extract ZIP File
If the file is a ZIP archive:
import zipfile
with zipfile.ZipFile(local_path, 'r') as zip_ref:
zip_ref.extractall(drive_folder)
print(f"Extracted to {drive_folder}")
🧩 For .tar, .tar.gz, or .rar files
Use tarfile for .tar or .tar.gz:
import tarfile
with tarfile.open(local_path, 'r:*') as tar:
tar.extractall(path=drive_folder)
Use rarfile for .rar (requires installing rarfile and unrar):
!apt install unrar
!pip install rarfile
import rarfile
rf = rarfile.RarFile(local_path)
rf.extractall(path=drive_folder)
✅ Done!
Now your extracted files are saved directly in your Google Drive under the folder MyDrive/ExtractedFiles.