Page 1 of 1

listdir function not included in os module for Unix build

Posted: Fri May 20, 2022 6:36 am
by MindJuice
I'm using os.listdir() and it's working just fine in my STM32 build, but it's not defined for some reason in the Unix build for my simulator.

My code is essentially the following:

Code: Select all

import os
for fn in os.listdir('/sd/'):
  # Some code here
  pass
This is the error that occurs:

Code: Select all

ArtributeError: 'module' object has no attribute 'listdir'
If I add a call like print(dir(os)) I can see that, in fact, there is no listdir, but there are other `os` functions there.

I suspect there is some MICROPY_PY_XXX flag that needs to be defined to cause listdir to be defined, but I can't seem to figure out what it would be.

Does anyone know?

Thanks!

Re: listdir function not included in os module for Unix build

Posted: Fri May 20, 2022 6:54 am
by aivarannamaa
I don't know why is it omitted, but you can use `ilistdir` instead: https://docs.micropython.org/en/latest/ ... s.ilistdir

Re: listdir function not included in os module for Unix build

Posted: Fri May 20, 2022 7:34 am
by bulletmark
There are plenty of modules not defined in the standard unix build. Do `make VARIANT=dev` instead to build micropython-dev which includes everything. Don't know why it is like this, just stumbled on it myself a few months ago.

Re: listdir function not included in os module for Unix build

Posted: Sat May 21, 2022 3:59 am
by MindJuice
I don't like doing this, but I've used ilistdir() in the interest of time, which does exist. Would be nice to undersand why listdir is missing though.

Thanks aivarannamaa for the suggestion. Funnily enough, it looks like I forgot having the same issue a while back, as I found another place where I used ilistdir in a function I wrote a year or more ago.

Re: listdir function not included in os module for Unix build

Posted: Sat May 21, 2022 4:00 am
by MindJuice
bulletmark wrote:
Fri May 20, 2022 7:34 am
There are plenty of modules not defined in the standard unix build. Do `make VARIANT=dev` instead to build micropython-dev which includes everything. Don't know why it is like this, just stumbled on it myself a few months ago.
Thanks for the suggestion. My simulator doesn't have the dev variant. I could possibly copy it over from elsewhere, but I decided to just use ilistdir for now.

Re: listdir function not included in os module for Unix build

Posted: Sat May 21, 2022 4:21 am
by bulletmark
MindJuice wrote:
Sat May 21, 2022 4:00 am
My simulator doesn't have the dev variant.
So your "simulator" is not a Linux PC?

Re: listdir function not included in os module for Unix build

Posted: Sat May 21, 2022 5:57 am
by MindJuice
It is a Linux PC, but there is no dev variant for the simulator. The way the original developer setup the makefiles is a bit weird and not the standard way MP does it. There is a `simulator/variant/simulator` folder which is used, rather than `ports/unix/variants/dev/`.