2017-07-29 130 views
0

没有发现我中提取d这4个文件:路径在根文件夹中现有的文件在Windows

train-images-idx3-ubyte 
train-labels-idx1-ubyte 
t10k-images-idx3-ubyte 
t10k-labels-idx1-ubyte 

我得到的错误:

FileNotFoundError: [Errno 2] No such file or directory: 'D:/train-labels-idx1-ubyte'` 

我的代码:

def load_mnist(path, kind='train'): 
    """Load MNIST data from `path`""" 
    path = "D:/" 
    labels_path = os.path.join(path, 
           '%s-labels-idx1-ubyte' % kind) 
    images_path = os.path.join(path, 
           '%s-images-idx3-ubyte' % kind) 

    with open(labels_path, 'rb') as lbpath: 
     magic, n = struct.unpack('>II', 
           lbpath.read(8)) 
     labels = np.fromfile(lbpath, 
          dtype=np.uint8) 

    with open(images_path, 'rb') as imgpath: 
     magic, num, rows, cols = struct.unpack(">IIII", 
               imgpath.read(16)) 
     images = np.fromfile(imgpath, 
          dtype=np.uint8).reshape(len(labels), 784) 

    return images, labels 

全错误:

Traceback (most recent call last): 
    File "C:/Users/PycharmProjects/MachineLearning/NN2.py", line 28, in <module> 
    X_train, y_train = load_mnist('mnist/', kind='train') 
    File "C:/Users/PycharmProjects/MachineLearning/NN2.py", line 14, in load_mnist 
    with open(labels_path, 'rb') as lbpath: 
FileNotFoundError: [Errno 2] No such file or directory: 'D:/train-labels-idx1-ubyte' 

这是渊源代码从教科书: https://github.com/rasbt/python-machine-learning-book/blob/master/code/ch12/ch12.ipynb

+0

只是,抱歉。链接包含作者的原始代码。 – econ

+1

我敢打赌这个文件有一个浏览器不显示的扩展名。 – ForceBru

+0

@ForceBru这意味着什么? – econ

回答

1

这可能是一个路径问题。如何通过命令行终端导航到你的D:文件夹,打开Python解释器那里,做了

import os 
os.listdir() 

显示所有文件D:下&文件夹。然后,您可以检查train-labels-idx1-ubyte是否确实存在,以及它是如何拼写的。

0

这里是溶液: https://www.reddit.com/r/learnpython/comments/6qc9t1/path_to_existing_file_in_root_folder_not_found_on/

这是错字HAHAH。对不起,我只是没有注意到。

+0

我看了看链接,问题似乎是,解压缩.gz文件时使用的工具在解压时改变了文件的名称(我的行为正确,没有)。关于此的一个教训是:*不要发布文字图像*。如果您已将文件名粘贴为文本,我们会立即注意到这一点,并省下大量时间。或者,至少,一张足够可读的图像会有所帮助。下次会更好! –

相关问题