Importing Zip File in Google Colaboratory Stored in Google Drive

Resuing a zip file from google colab

For anyone wondering how to do this...

first, zip you file containing your images and upload to Google Drive

second, in Google Colab, mount your drive

from google.colab import drive
drive.mount('/content/drive')

third, copy the zip folder into your local environment

!cp "/file path with the zip image folder.zip" "whatever you want to      call the file when unzipped"

fourth, unzip in local colab into a folder you made

!unzip -uq "whatever you want to call the file when unzipped" -d "folder where you want them unzipped (or just make one up)"

You will have to do this every time, but it is super quick compared to what I was doing before

How to access zip files from drive on Google collab?

Skip PyDrive and mount your Google Drive directly using this snippet:

from google.colab import drive
drive.mount('/content/drive')

Then, you can access directories in Drive just as though they were normal files on the backend VM.

Import data into Google Colaboratory

An official example notebook demonstrating local file upload/download and integration with Drive and sheets is available here:
https://colab.research.google.com/notebooks/io.ipynb

The simplest way to share files is to mount your Google Drive.

To do this, run the following in a code cell:

from google.colab import drive
drive.mount('/content/drive')

It will ask you to visit a link to ALLOW "Google Files Stream" to access your drive. After that a long alphanumeric auth code will be shown that needs to be entered in your Colab's notebook.

Afterward, your Drive files will be mounted and you can browse them with the file browser in the side panel.

enter image description here

Here's a full example notebook

How to open and work on files stored in Google drive from Google colab?

  1. First you need to mount google drive and then

    from google.colab import drive

    drive.mount('/content/gdrive')

  2. Click the link on colb and authenticate (copy password and paste in colab) to mount and access your gdrive.

  3. You can access your folder as shown below
    path_to_data = '/content/gdrive/My Drive/MyFolder_where_data_stored'
    Unique_Labels_List = os.listdir(path_to_data)

  4. After running model, save the model, zip and download to local.

    !zip -r ./MyFolder.zip /content/MyFolder

    files.download("/content/MyFolder.zip")

There are many other things you can do. Check this resource for some more commands and you can find many more by searching in google. Thanks!

My colab won't let me import my dataset folder in drive

Is /content/gdrive/MyDrive/Colab Notebooks/PetImages a ZIP file or is this the folder containing the ZIP file you want to extract?

The error message says that you are not passing a ZIP file but a folder.

So probably:

zf = zipfile.ZipFile(DATA_PATH + "PetImages/images.zip", "r")
zf.extractall(DATA_PATH)

upload file to another Drive Google colab

I finally found a solution through the PyDrive library.

Here is an example for the curious little ones ;)

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

gfile = drive.CreateFile({'parents': [{'id': 'ID folder'}]})
gfile.SetContentFile("file path")
gfile.Upload()

Once the imports and the creation of the gauth variable have been executed, you will be asked for permissions on your account (don't be surprised)

Hope this helps anyone in need !



Related Topics



Leave a reply



Submit