Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python cannot find a directory

I wrote a code like this with jupyter notebook in a project;

import os


image_path = r'C:\Users\ays\Desktop\IR\01.jpg'
image_files = os.listdir(image_path)
img = cv2.imread(os.path.join(image_path,image_files))
cv2.imshow('image',img)

it gives an error like;

[WinError 3] The system cannot find the path specified: ‘C:\Users\ays\Desktop\IR\01.jpg’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i was trying to print an image and
i had a directory problem

>Solution :

The argument to os.listdir() must be the directory, not the image file, so remove \01.jpg.

Then you’ll need to loop over the result of os.listdir(), since it returns a list.

import os

image_path = r'C:\Users\ays\Desktop\IR'
image_files = os.listdir(image_path)
for file in image_files:
    img = cv2.imread(os.path.join(image_path,file))
    cv2.imshow('image',img)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading