59

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?

Anil Kumar
  • 348
  • 1
  • 12
tetedp
  • 713
  • 1
  • 6
  • 9

5 Answers5

174

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
  • 1,849
  • 1
  • 6
  • 4
  • 2
    I'd mark this answer as a solution. It completely works for me. Thank you – Dmitry Anch Jan 06 '20 at 13:13
  • 2
    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
8

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.

Ivan
  • 529
  • 8
  • 12
3

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
  • 321
  • 1
  • 5
3

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,277
  • 4
  • 8
  • 25
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
  • 26,929
  • 9
  • 72
  • 69