FileNotFoundError:[WinError 3]系统找不到指定的路径

时间:2019-09-29 14:37:19

标签: python-3.x image-processing jupyter-notebook

代码:

import os
import cv2
folder = ['test images', 'ALB', 'BET', 'DOL', 'LAG', 'NoF', 'OTHER', 
'SHARK', 'YFT' ]
Path = r'D:\ncfm\train'

for i in range(9):
    listing = os.listdir(path+'/'+folder[i])
    folder[i+1] = np.array([np.array(cv2.imread(path+'/'+folder[i]+'/'+file)).flatten()
for file in listing])

错误:

FileNotFoundError                         Traceback (most recent call 
last)
<ipython-input-152-d8f8c2149488> in <module>
      5 
      6 for i in range(9):
----> 7     listing = os.listdir(path+'/'+folder[i])
      8     folder[i+1] = 
np.array([np.array(cv2.imread(path+'/'+folder[i]+'/'+file)).flatten()
      9     for file in listing])

FileNotFoundError: [WinError 3] The system cannot find the path specified: 
'Users\\USER\\Desktop\\ncfmtrain\\YFT\\*.jpg/test images'

我已尝试纠正许多次。但是问题仍然存在。然后我尝试了对我有用的这段代码。

import os
from os import listdir

for i in range(9):
    for fld in folders:
        index = folders.index(fld)
        print('Load folders {} (Index: {})'.format(fld, index))
        path = os.path.join('Users', 'USER' , 'Desktop','ncfm' 'train', fld, '*.jpg')
        L.append(len(path))


    break

这对我来说很好。但随后出现以下错误:

  

ValueError: Found input variables with inconsistent numbers of samples: [8, 3777]

我想这些是相关的。

1 个答案:

答案 0 :(得分:0)

使用pathlib进行文件系统访问。

from pathlib import Path

jpeg_images = list(Path('D:/ncfm/train').glob('**/*.jpg'))
print(jpeg_images)
np.array([np.array(cv2.imread(str(file))).flatten() for file in jpeg_images])

更新:

测试一个文件

  • 使用文件的完整路径,包括驱动器号。
file = Path(r'C:\Users\USER\Desktop\ncfmtrain\YFT\image_name.jpg')
print(cv2.imread(str(file))).flatten())