0

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    Could you paste your formatted code directly here ? – Mohan Radhakrishnan Apr 18 '22 at 11:47
  • The immediate problem is probably that you are treating all entries in the directory as subdirectories, but there are some items which are files, and so you can't add a slash to the end of those. But this code has several other problems, too. We can't know which files actually exist on your system so I'm voting to close as lacking details. – tripleee Apr 19 '22 at 04:47

1 Answers1

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")
High-Octane
  • 1,104
  • 5
  • 19
  • Tried it, but getting the same error. – Apeksha Rathnamali Apr 18 '22 at 12:15
  • @ApekshaRathnamali I am assuming that it's an physical directory error, it's not being detected as a directory. Can you share the directory structure and the updated script here. – High-Octane Apr 18 '22 at 12:56
  • The `os.getcwd()` call is completely useless here; see [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory) – tripleee Apr 19 '22 at 04:38