Search code examples
gzippickle

Why doesnt gzip.open() recognize my compressed file?


I have been trying to extract a large set of images that are in a .pkl.gz file. Here is my code for doing so.

import gzip
import pickle
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import os

for f in os.listdir("W:\Code\Machine Learning\digit-recognition-dnn\data"):
    print(f)

name = 'mnist.pkl.gz'

with gzip.open(name, 'rb') as f:
    train_set, valid_set, test_set = pickle.load(f)

train_x, train_y = train_set

plt.imshow(train_x[0].reshape((28, 28)), cmap=cm.Greys_r)
plt.show()

Unfortunately, the output is like this:

getData.py
mnist.pkl.gz
Traceback (most recent call last):
  File "w:\Code\Machine Learning\digit-recognition-dnn\data\getData.py", line 12, in <module>
    with gzip.open(name, 'rb') as f:
  File "C:\Users\trexx\AppData\Local\Programs\Python\Python39\lib\gzip.py", line 58, in open
    binary_file = GzipFile(filename, gz_mode, compresslevel)
  File "C:\Users\trexx\AppData\Local\Programs\Python\Python39\lib\gzip.py", line 173, in __init__
    fileobj = self.myfileobj = builtins.open(filename, mode or 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'mnist.pkl.gz'

My filetree looks like this:

data
├─ getData.py
└─ mnist.pkl.gz

Any suggestions?

I tried changing name to `

name = 'mnist.pkl'

and

name = 'mnist.gz'

The result is the same. FileNotFound.


Solution

  • Try specifying a path instead of just the name.

    Replace mnist.pkl.gz with something like "W:\my_dir\mnist.pkl.gz