2017-07-29 133 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文件時使用的工具在解壓時改變了文件的名稱(我的行爲正確,沒有)。關於此的一個教訓是:*不要發佈文字圖像*。如果您已將文件名粘貼爲文本,我們會立即注意到這一點,並省下大量時間。或者,至少,一張足夠可讀的圖像會有所幫助。下次會更好! –

相關問題