Search code examples
datasetkaggle

How to download kaggle dataset?


How do I download kaggle datasets to colab or any other place from script or notebook?

! kaggle datasets download -d arslanali4343/world-cities-database-population-oct2022


Solution

  • You can try this one using Colab

    Step 1:

    from google.colab import files
    
    files.upload() # <<-- Upload your Kaggle API-Token 
    
    ! mkdir ~/.kaggle # <<-- Create a Kaggle folder inside Colab environment 
    
    ! cp kaggle.json ~/.kaggle/  # <<- Gets your Token into it
    
    ! chmod 600 ~/.kaggle/kaggle.json  # <<- Gives Colab permissions
    
    ! kaggle competitions download -c <competition_name>
    
    file_path = '/content/<competition_file_name>'
    

    After that, you'll get a .zip file, so...

    Step 2:

    from zipfile import ZipFile
    
    with ZipFile(file_path, 'r') as unzip:
    
        unzip.printdir() # <<-- Check inside zip file
    
        unzip.extract('train.csv') # <<-- Extract a single file
    
        #unzip.extractall() # <<-- Extract all files at once
        
    

    You can also use a submission command to upload your files straight to Kaggle servers

    ! kaggle competitions submit <competition_name> -f <submission_file> -m "My submission message"
    

    Have fun