'NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\\masksdetection-master//Recog_Train/.DS_Store/'

This is the code, and when I ran it I got the below NotADirectoryError. I tried to replace the directory but I got the same error. Can someone please help me to solve the issue.

Code:

import os

img_path = os.path.join(os.getcwd(), "Recog_Train")

class_names = []

for subdir in os.listdir(img_path):
    path = img_path + '/' + subdir
    path = path + '/'
    for img in os.listdir(path):
        img_pic = path + img
        class_names.append(subdir)

Error:

PS C:\masksdetection-master> python -u "c:\masksdetection-master\Face_recog.py"
2022-04-18 16:23:22.527773: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2022-04-18 16:23:22.537982: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
  File "c:\masksdetection-master\Face_recog.py", line 33, in <module>
    for img in os.listdir(path):
NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\\masksdetection-master//Recog_Train/.DS_Store/'

Eventually I would like to process these images with OpenCV and Tensorflow.



Solution 1:[1]

Looks like a path error, Try using the 2 solutions below:

This should work if your file is in the same run directory

import os
from pathlib import Path

img_path = os.path.join(os.getcwd(), "Recog_Train")

But ideally I would suggest using this below, if the files are in project directory because it will allow you to run from anywhere.

import os
from pathlib import Path

img_path = os.path.join(str(Path(__file__).parent), "Recog_Train")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1