在 Windows 上找不到根文件夹中现有文件的路径



>我在D中提取了这4个文件:

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

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

import os
os.listdir()

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

这是解决方案: https://www.reddit.com/r/learnpython/comments/6qc9t1/path_to_existing_file_in_root_folder_not_found_on/

是错别字哈哈。对不起大家,我只是没有注意到。

最新更新