本文介绍了FileNotFoundError:[WinError 3]系统找不到指定的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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'

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

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:

我想这些是相关的.

推荐答案

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

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

更新:

  • 摘自OP 'Users\\USER\\Desktop\\ncfmtrain\\YFT\*.jpg/test images'的评论
    • 您没有正确使用pathlib
    • pathlib
    • Python 3的pathlib模块:驯服文件系统
    • 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
        • 测试一个文件

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

          这篇关于FileNotFoundError:[WinError 3]系统找不到指定的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 13:19