簡體   English   中英

在 python WindowsError: [錯誤 3] 系統找不到指定的路徑:'F:/Dataset\\*.*'

[英]In python WindowsError: [Error 3] The system cannot find the path specified: 'F:/Dataset\\*.*'

for category in CATEGORIES:  # do dogs and cats
    path = os.path.join(DATADIR,category)  # create path to dogs and cats
    for img in os.listdir(path):  # iterate over each image per dogs and cats
        img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE)  # convert to array
        plt.imshow(img_array, cmap='gray')  # graph it
        plt.show()  # display!

發生了錯誤:

WindowsErrorTraceback (most recent call last)
<ipython-input-4-be2d68025f07> in <module>()
      1 for category in CATEGORIES:  # do dogs and cats
      2     path = os.path.join(DATADIR,category)  # create path to dogs and cats
----> 3     for img in os.listdir(path):  # iterate over each image per dogs and cats
      4         img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE)  # convert to array
      5         plt.imshow(img_array, cmap='gray')  # graph it

WindowsError: [Error 3] The system cannot find the path specified: 'F:/Dataset\\Bening\\*.*'
WindowsError: [Error 3] The system cannot find the path specified: 'F:/Dataset\\Bening\\*.*'

該錯誤字面意思是路徑錯誤。

讓我們更仔細地看一下:

我們有一個由'F:/Dataset\\Bening\\*.*'引起的錯誤的os.listdir(path)

首先, Python 中的路徑不使用通配符 . 這是一個os.listdir ,所以你在那里傳遞一個目錄。 如果您想使用通配符,請嘗試glob

其次,您在DATADIR的定義中使用了錯誤的分隔符 - os.path.join使用系統特定的分隔符,即 Windows 中的反斜杠(打印中的雙反斜杠,因為它需要轉義),您使用正常的正斜杠。 您可以通過在打開字符串之前放置r來放置帶有反斜杠的路徑(這意味着字符串是原始的 - 反斜杠是文字,而不是 escaping 序列)或手動加倍反斜杠。 r'F:\Dataset''F:\\Dataset

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM