143

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?

user
  • 1,220
  • 1
  • 12
  • 31
tetedp
  • 1,553
  • 3
  • 11
  • 9

11 Answers11

357

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")
Shashank Mishra
  • 3,687
  • 1
  • 9
  • 4
  • 5
    It is also worth noting, if you have problems downloading a large file over `files.download()`, you may also click on the chevron-right icon at the top left corner and browse all files under the `Files` tab and manually download the file. – Danny Jan 19 '20 at 13:38
  • If you ask: "Where is file.zip?" you could find it at the bottom on the left pane of the Colab Notebook. Then you could download it by right-clicking on it. – iedmrc Jul 20 '20 at 10:53
  • my filename is in python . . . how do you translate my python code into that format – Jitin Aug 01 '20 at 07:03
  • For me for some reason it stuck on `files.download`(.zip size ~2mb), but I was able to download it via `Files` manually. – mrgloom Aug 05 '21 at 16:06
35

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

ppwater
  • 2,315
  • 4
  • 15
  • 29
12

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.

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
Ivan
  • 759
  • 10
  • 18
10
!zip -r /content/sample_data.zip /content/sample_data
# change sample_data.zip to your desired download name Ex: nothing.zip
# change sample_data to your desired download folder name Ex: ner_data 

enter image description here

Aravind R
  • 835
  • 1
  • 7
  • 14
8

In my case, I had to download an entire folder containing h5 files(for submitting a college project) of each model my notebook built. Easiest way I found to download this folder and hence all files in the folder is to drag and drop the folder into the "My Drive" folder in the same folder tree.

Drive folder and folder to be uploaded highlighted

Obviously I later downloaded the folder from Google Drive.

Sneha Valabailu
  • 115
  • 2
  • 5
6

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 )
Shubham Panchal
  • 4,061
  • 2
  • 11
  • 36
  • 1
    If you don't have a folder of files and everything is on the main directory of Colab, you can use ```dir_to_zip = '/content' #@param {type: "string"}``` for the above code. That is the main dir of Colab. – Cagri Nov 25 '21 at 21:53
  • Another question: How can I select a few files to zip? All my files are on the main dir and I only need to zip 4 of them. How can I specifically zip just those? – Cagri Dec 03 '21 at 02:46
4

Copy this code into a cell, and change the 2 fields filename and folders_or_files_to_save. It will zip all of the folders or files into a zipfile and save it in your Google drive

#@title save yo data to drive
filename = "kerasmodel" #@param {type:"string"}
folders_or_files_to_save = "keras_model.h5" #@param {type:"string"}
from google.colab import files
from google.colab import auth
from googleapiclient.http import MediaFileUpload
from googleapiclient.discovery import build

def save_file_to_drive(name, path):
    file_metadata = {
    'name': name,
    'mimeType': 'application/octet-stream'
    }

    media = MediaFileUpload(path, 
                  mimetype='application/octet-stream',
                  resumable=True)

    created = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()

    print('File ID: {}'.format(created.get('id')))

    return created


extension_zip = ".zip"

zip_file = filename + extension_zip

# !rm -rf $zip_file
!zip -r $zip_file {folders_or_files_to_save} # FOLDERS TO SAVE INTO ZIP FILE

auth.authenticate_user()
drive_service = build('drive', 'v3')

destination_name = zip_file
path_to_file = zip_file
save_file_to_drive(destination_name, path_to_file)
skaem
  • 411
  • 2
  • 7
1

Use tar to group files in a directory into a single file.

For example, here's a snippet that creates a directory, 3 files inside it, and a .tar archive containing the group:

!mkdir demo
!echo a > demo/a
!echo b > demo/b
!echo c > demo/c
!tar -cvf demo.tar demo/

The file to download would be demo.tar in this case. For more tips, search for creating and expanding tar archives.

Bob Smith
  • 36,107
  • 11
  • 98
  • 91
1

Use the copy path for the folder which you want to download. Then:

from google.colab import files
files.download("path")
Baran
  • 63
  • 5
0

The following solution worked for me with low latency:

Step 1: Mount the google drive.

Step 2: Zip the content.

!zip -r destination_path_to_zip_file.zip source_path_to_zip

Step 3: Copy the zip file to the google drive.

!cp source destination

Step 4: Download the file.

Patriotic
  • 2,103
  • 4
  • 26
  • 36
-1
  1. If Drive and Colab Notebook both are your private:

    • a) Mount the Drive,
    • b) travers the path to your folder, click on "..." in front of the file name and use the path to the file/folder
  2. If You want to download a special folder without mounting Drive:

    • a) make a zip file of your folder (for single file no need for zipping)

    • b) !gdown --id zip_file_id 'like this id 1ZxqBjXlF0H6.....'

    • c) for file_id

      1-- -----use Get Link or Share of your ziped file --- enter image description here 2-- -------ID part of your file share link-------

enter image description here

Vahab
  • 307
  • 4
  • 7