I want to run a notebook that uses many header files defined in the directory. So basically I want to upload the entire directory to Google Colab so that I can run the notebook. But I am unable to find any such options and only able to upload files not complete folders. So can someone tell me how to upload entire directory to google colab?
-
1Does this answer your question? [Import data into Google Colaboratory](https://stackoverflow.com/questions/46986398/import-data-into-google-colaboratory) – theProcrastinator Aug 09 '21 at 06:41
8 Answers
I suggest you not to upload them just in Colab, since when you're restarting the runtime you will lose them (just need to re-upload them, but it can be an issue with slow connections).
I suggest you to use the google.colab
package to manage files and folders in Colab. Just upload everything you need to your google drive, then import:
from google.colab import drive
drive.mount('/content/gdrive')
In this way, you just need to login to your google account through google authentication API, and you can use files/folders as if they were uploaded on Colab.
EDIT May 2022:
As pointed out in the comments, using Google Drive as storage for a large number of files to train a model is painfully slow, as described here: Google Colab is very slow compared to my PC. The better solution in this case is to zip the files, upload them to colab and then unzip them using
!unzip file.zip
More unzip options here: https://linux.die.net/man/1/unzip

- 1,046
- 12
- 21
-
Hi, How can I do to copy 2 small files to a specifil folder in google colab. I appreciate any comment o help. Thank you – Fernando Jun 07 '20 at 15:54
-
2
-
-
4Using Google Drive as storage for a large number of files to train a model is painfully slow as described https://stackoverflow.com/questions/49360888/google-colab-is-very-slow-compared-to-my-pc. The better solution is to zip the files, upload them to colab and then unzip them as the next answer suggests. – Lehrian May 28 '22 at 22:04
The easiest way to do this, if the folder/file is on your local drive:
- Compress the folder into a ZIP file.
- Upload the zipped file into colab using the upload button in the File section. Yes, there is a File section, see the left side of the colab screen.
- Use this line of code to extract the file. Note: The file path is from colab's File section.
from zipfile import ZipFile
file_name = file_path
with ZipFile(file_name, 'r') as zip:
zip.extractall()
print('Done')
- Click Refresh in the colab File section.
- Access the files in your folder through the file paths
Downside: The files will be deleted after the runtime is over.
You can use some part of these steps if your file is on a Google Drive, just upload the zipped file to colab from Google Drive.

- 7,102
- 69
- 48
- 77

- 149
- 1
- 3
you can create a git repository and push the files and folders to it, and then can clone the repository in colaboratory with the command
!git clone https://github.com/{username}/{projectname}.git
i feel this method is faster. but if the file size is more than 100 mb you will have to zip the file or will have to add extentions to push it to github. for more information refer the link below.
https://help.github.com/en/github/managing-large-files/configuring-git-large-file-storage

- 337
- 2
- 7
-
1Please provide, at least, highlights from the referenced URL in case of it being broken in the future. – Okhan Okbay Apr 15 '20 at 19:51
The best way to approach this problem is simple yet tricky sometimes.
You first need to compress the folder into a zipped file and upload the same into your google drive.
While doing so, Make sure that the folder is in the root directory of the drive and not in any other subfolder!. If the compressed folder/data is in other subfolder, you can easily move the same into the root directory.
Compresses folder/data in another subfolder often messes with the unzipping process when you will be specifying the file location.
Once you did the afore mentioned tasks, enter the following commands in the colab to mount your drive:
from google.colab import drive
drive.mount('/content/gdrive')
This will ask for an access token that can be generated by clicking on the url displayed in the output of the same cell
!ls gdrive/MyDrive
Check the contents of the drive by executing the above command and ensure that your folder/data is displayed in the output.
!unzip gdrive/MyDrive/<File_name_without_space>.zip
eg:
!unzip gdrive/MyDrive/data_folder.zip
Executing the same will start unzipping your folder into the memory.
Congrats! You have successfully uploaded your folder/data into the colab.

- 1,418
- 1
- 7
- 5
zip your files zip -r file.zip your_folder
and then:
from google.colab import files
from zipfile import ZipFile
with ZipFile(files.upload(), 'r') as zip:
zip.extractall()
print('Done')

- 1
- 1
import zipfile
import os
import shutil
# Path to the zip file on your Google Drive
zip_file_path = '/content/hymenoptera_data.zip'
# Path to the desired location where you want to move the contents
desired_location = '/content/'
# Unzip the folder
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
zip_ref.extractall('/content/')
# Get the name of the unzipped folder (assuming there's only one folder)
unzipped_folder_name = os.listdir('/content/')[0]
# Move the contents to the desired location
source_path = '/content/' + unzipped_folder_name
destination_path = desired_location + unzipped_folder_name
shutil.move(source_path, destination_path)
print("Unzipping and moving completed successfully!")
Made by ChatGPT. (Different Prompt)
- Step 1: Upload the zip (Directly on colab or drive)
- Step 2: Change the Paths.
- Step 3: Run.

- 281
- 3
- 12
So here's what you can do: -upload the dataset desired folder to your drive -over colab, mount the drive wherein this "from google.colab import drive drive.mount('/content/gdrive')" automatically shows up and you just need to run it -then check for your file over the Files section on the left-hand side (if folder not visible try refreshing, also there should be a drop-down arrow next to it where you can check all the files under the folder ) -left-click over the folder wherein you get a COPY PATH option -paste the copied path over the desired location in your colab

- 1
- 1