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?
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 :-).
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.
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.
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")
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
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.
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 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With