Extract Google Drive zip from Google colab notebook

前端 未结 12 1452
醉梦人生
醉梦人生 2020-12-25 12:29

I already have a zip of (2K images) dataset on a google drive. I have to use it in a ML training algorithm. Below Code extracts the content in a string format:



        
相关标签:
12条回答
  • 2020-12-25 13:04

    Instead of GetContentString(), use GetContentFile() instead. It will save the file instead of returning the string.

    downloaded.GetContentFile('images.zip') 
    

    Then you can unzip it later with unzip.

    0 讨论(0)
  • 2020-12-25 13:08

    You can simply use this

    !unzip file_location
    
    0 讨论(0)
  • 2020-12-25 13:13

    Mount GDrive:

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

    Open the link -> copy authorization code -> paste that into the prompt and press "Enter"

    Check GDrive access:

    !ls "/content/gdrive/My Drive"
    

    Unzip (q stands for "quiet") file from GDrive:

    !unzip -q "/content/gdrive/My Drive/dataset.zip"
    
    0 讨论(0)
  • 2020-12-25 13:15

    To extract Google Drive zip from a Google colab notebook:

    import zipfile
    from google.colab import drive
    
    drive.mount('/content/drive/')
    
    zip_ref = zipfile.ZipFile("/content/drive/My Drive/ML/DataSet.zip", 'r')
    zip_ref.extractall("/tmp")
    zip_ref.close()
    
    0 讨论(0)
  • 2020-12-25 13:20

    Try this:

    !unpack file.zip
    

    If its now working or file is 7z try below

    !apt-get install p7zip-full
    !p7zip -d file_name.tar.7z
    !tar -xvf file_name.tar
    

    Or

    !pip install pyunpack
    !pip install patool
    
    from pyunpack import Archive
    Archive(‘file_name.tar.7z’).extractall(‘path/to/’)
    !tar -xvf file_name.tar
    
    0 讨论(0)
  • 2020-12-25 13:22

    SIMPLE WAY TO CONNECT

    1) You'll have to verify authentication

    from google.colab import auth
    auth.authenticate_user()
    from oauth2client.client import GoogleCredentials
    creds = GoogleCredentials.get_application_default()
    

    2)To fuse google drive

    !apt-get install -y -qq software-properties-common python-software-properties module-init-tools
    !add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
    !apt-get update -qq 2>&1 > /dev/null
    !apt-get -y install -qq google-drive-ocamlfuse fuse
    

    3)To verify credentials

    import getpass
    !google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
    vcode = getpass.getpass()
    !echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
    

    4)Create a drive name to use it in colab ('gdrive') and check if it's working

    !mkdir gdrive
    !google-drive-ocamlfuse gdrive
    !ls gdrive
    !cd gdrive
    
    0 讨论(0)
提交回复
热议问题