SmuudiNi
Posts: 4
Joined: Sun Mar 25, 2018 4:23 pm

Issue with reading directory on raspberry

Sun Mar 25, 2018 4:31 pm

Hello!

I'm pretty new to python in general.

Im currently having some issue with the os package in python. This code works if i run it trought the terminal on my mac, but gives an error when ran on the raspberry pi 3 where i get a `FileNotFoundError: [Errror 2] No such file or directory: '../images/' [Error].

Code: Select all

def getImages():
    currentPath = os.path.dirname(os.path.abspath(__file__)); # Absolute dir the script is in
    filepath = "../images/"; # The path where the pictures are uploaded
    directory = os.listdir(os.path.join(currentPath, filepath));
    images = [ fi for fi in directory if fi.endswith(('.JPG', '.jpg', 'jpeg', '.JPEG')) ];
    return images;
My folder structure is like this:
- python/face-recognition/python-script.py
- python/images/

If needed, the whole code can be watched here:
https://github.com/smajohusic/work-in-p ... ize.py#L18

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: Issue with reading directory on raspberry

Sun Mar 25, 2018 5:14 pm

I don't know if this is the cause of your problem, but you have semicolons after each statement in your getImages() definition. They shouldn't be there.

SmuudiNi
Posts: 4
Joined: Sun Mar 25, 2018 4:23 pm

Re: Issue with reading directory on raspberry

Sun Mar 25, 2018 5:51 pm

Tried to remove all ; but still the same issue with not finding the directory.

User avatar
typematrix
Posts: 20
Joined: Sun Jul 02, 2017 3:55 pm
Location: Europe
Contact: Website

Re: Issue with reading directory on raspberry

Sun Mar 25, 2018 5:58 pm

Hi

I just tested code on a pi 3 , it runs fine.

Double check spelling of your directory tree I suggest does images have a capital I

Can you post entire error message?

ps
You should run a python linting tool on your code to help improve it.

regards
Last edited by typematrix on Sun Mar 25, 2018 6:05 pm, edited 1 time in total.

SmuudiNi
Posts: 4
Joined: Sun Mar 25, 2018 4:23 pm

Re: Issue with reading directory on raspberry

Sun Mar 25, 2018 6:05 pm

typematrix wrote:
Sun Mar 25, 2018 5:58 pm
Hi

I just tested code it runs fine.

Double check spelling of your directory tree I suggest.

Can you post entire error message?

ps
You should run a python linting on your code to help improve it.

regards
Did you run it trough the Raspberry?
I have added the stack trace of the error thrown when executing the code on Raspberry Pi 3
IMG_1156.JPG
IMG_1156.JPG (101.99 KiB) Viewed 890 times

User avatar
B.Goode
Posts: 8841
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Issue with reading directory on raspberry

Sun Mar 25, 2018 6:06 pm

I don't have an answer.

But to help you find one -

The official documentation is here:
https://docs.python.org/3/library/os.path.html

(There is a Python2 version if you need that.)

My hunch is that the ../images syntax may not be treated as you expect.

Since the code involved is so small, my approach would be to fire up an interactive Python3 interpreter session and type the statements into it and check the values that are returned. Or, equivalently, use the default python shell window that is opened by default by the IDLE3 IDE.

Perhaps you need to find the parent of your working directory by some means other than " .. " before appending images to it. (My understanding is that the os.path library provides the / separators.)

User avatar
B.Goode
Posts: 8841
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Issue with reading directory on raspberry

Sun Mar 25, 2018 6:09 pm

Follow up: just seen your screen shot which indeed proves that ../ images has been treated as a literal string, rather than as a relative filestore reference.

User avatar
typematrix
Posts: 20
Joined: Sun Jul 02, 2017 3:55 pm
Location: Europe
Contact: Website

Re: Issue with reading directory on raspberry

Sun Mar 25, 2018 6:20 pm

Ya i ran it on a pie 3



whats output of command
ls -la ../images

run from
from face-recognition folder

SmuudiNi
Posts: 4
Joined: Sun Mar 25, 2018 4:23 pm

Re: Issue with reading directory on raspberry

Sun Mar 25, 2018 6:32 pm

B.Goode wrote:
Sun Mar 25, 2018 6:06 pm
I don't have an answer.

But to help you find one -

The official documentation is here:
https://docs.python.org/3/library/os.path.html

(There is a Python2 version if you need that.)

My hunch is that the ../images syntax may not be treated as you expect.

Since the code involved is so small, my approach would be to fire up an interactive Python3 interpreter session and type the statements into it and check the values that are returned. Or, equivalently, use the default python shell window that is opened by default by the IDLE3 IDE.

Perhaps you need to find the parent of your working directory by some means other than " .. " before appending images to it. (My understanding is that the os.path library provides the / separators.)
I feel like an idiot now, but you are correct in assuming that /images folder does not exists.
Checking the images folder i have a .gitkeep and .gitignore that ignores all files, (even .gitkeep), and that's why it threw an error because it removed the folder when cloning the repo on the raspberry pi.

Oh well, at least i learned something today, thanks!

User avatar
B.Goode
Posts: 8841
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Issue with reading directory on raspberry

Sun Mar 25, 2018 6:37 pm

I feel like an idiot now, but you are correct in assuming that /images folder does not exists.
Actually, I think that gem came from @typematrix.

Pleased that you are on the right track again.

Return to “Python”