FileNotFoundError: [WinError 3] The system cannot find the path specified

梦想的初衷 提交于 2019-12-25 00:28:55

问题


Code:

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])

Error:

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'

i have tried to rectify this many times. but the problem still exist. then i tried this code which worked for me.

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

This is working fine for me. But then comes the following error:

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

I guess these are related.


回答1:


Use pathlib for filesystem access.

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])

Update:

  • From this comment by the OP 'Users\\USER\\Desktop\\ncfmtrain\\YFT\*.jpg/test images'
    • You are not properly using pathlib
    • pathlib
    • Python 3's pathlib Module: Taming the File System

Test One File

  • Use the entire path to your file, including the drive letter.
file = Path(r'C:\Users\USER\Desktop\ncfmtrain\YFT\image_name.jpg')
print(cv2.imread(str(file))).flatten())


来源:https://stackoverflow.com/questions/58156230/filenotfounderror-winerror-3-the-system-cannot-find-the-path-specified

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!