How did I read the .gz file with colab

Viewed 4801

I am a beginner in python, I am working with Google colab, I downloaded a .gz file and saved it in my desktop from MNIST database, when I want to read this file:

import tensorflow as tf
import zipfile
with zipfile.ZipFile("C:/Users/hadi.abidi/Desktop/train-images-idx3-ubyte (2).gz") as z:
  with z.open("C:/Users/hadi.abidi/Desktop/train-images-idx3-ubyte (2).gz") as f:
    train = pd.read_csv(f,header=0,delimiter="\t")
    print(train.read())

FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/hadi.abidi/Desktop/train-images-idx3-ubyte (2).gz'
2 Answers

Please check the path correctly and also check with below code

import gzip
f=gzip.open("C:/Users/hadi.abidi/Desktop/train-images-idx3-ubyte (2).gz",'rb')
file_content=f.read()
print(file_content)

You need to upload the file on to Colab server first:

from google.colab import files
files.upload()

This renders a widget to upload files. From there on you can run the code for bringing the content of the file on to your notebook:

with zipfile.ZipFile("train-images-idx3-ubyte (2).gz") as z:
...

This approach requires that you interact with your notebook whenever you start your notebook. In order to have a seamless execution, I would suggest to upload the file onto Github and retrieve the file from there.

Related