Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I download multiple files or an entire folder from Google Colab?

Currently, I can download files as individual files with the command

files.download(file_name)

I also tried uploading them to the drive with the below code snippet but it is uploading them as individual files.

uploaded = drive.CreateFile({'title': file_name})
uploaded.SetContentString('Sample upload file content')
uploaded.Upload()
print('Uploaded file with ID {}'.format(uploaded.get('id')))

How can I download multiple files as a folder to my local computer? Or how can I upload these files as a folder to my google drive?

like image 549
tetedp Avatar asked May 21 '18 16:05

tetedp


People also ask

How do I download all files from a folder in Colab?

How to Download Folders from Colab. The command is !zip followed by r which means “recursive”, then we write the file path of the zipped file (i.e. /content/sample_data. zip ) and finally, we write the folder that we want to zip (i.e. /content/sample_data ) and voila, the zip file is generated :-).

How do I download directly from Google Colab?

Step #1 : Sign in to Google Colab and Create a new Python3 notebook. To import google drive, write this code in code section of colab and run it by Ctrl+Enter . On running code, one blue link and a text box will appear we need to provide a permission text.

How do I download a folder from Google Drive link Colab?

Create a shortcut to this file in your Google Drive (right-click on file -> "Add shortcut to Disk") Mount your Google drive in Colab notebook (click buttons: "Files" -> "Mount Drive") Now you can access this file via your shortcut for ! unzip/np.


4 Answers

I have created a zip file:

!zip -r /content/file.zip /content/Folder_To_Zip

Than I have downloded that zip file:

from google.colab import files
files.download("/content/file.zip")
like image 106
Shashank Mishra Avatar answered Oct 13 '22 19:10

Shashank Mishra


For example, if you have to download log folder:

!zip -r log.zip log/

-r represent recursive

while log.zip is destination zip file and log/ is source folder path

enter image description here

like image 44
Muhammad Ali Abbas Avatar answered Oct 13 '22 17:10

Muhammad Ali Abbas


I found that:

!zip -r ./myresultingzippedfolderwithallthefiles.zip ./myoriginalfolderwithallthefiles/

worked for me in colab.

Here . can be your home directory or the directory where your original myoriginalfolderwithallthefiles is and where myresultingzippedfolderwithallthefiles.zip will be created. Change the directories as needed.

like image 12
Ivan Avatar answered Oct 13 '22 19:10

Ivan


You may use code to zip folders and download them using files.

#@title Utility to zip and download a directory
#@markdown Use this method to zip and download a directory. For ex. a TB logs 
#@markdown directory or a checkpoint(s) directory.

from google.colab import files
import os

dir_to_zip = 'dir_name' #@param {type: "string"}
output_filename = 'file.zip' #@param {type: "string"}
delete_dir_after_download = "No"  #@param ['Yes', 'No']

os.system( "zip -r {} {}".format( output_filename , dir_to_zip ) )

if delete_dir_after_download == "Yes":
    os.system( "rm -r {}".format( dir_to_zip ) )

files.download( output_filename )
like image 6
Shubham Panchal Avatar answered Oct 13 '22 18:10

Shubham Panchal