listdir function not included in os module for Unix build

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
MindJuice
Posts: 18
Joined: Tue Apr 14, 2020 6:09 am

listdir function not included in os module for Unix build

Post by MindJuice » Fri May 20, 2022 6:36 am

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!

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

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

Post by aivarannamaa » Fri May 20, 2022 6:54 am

I don't know why is it omitted, but you can use `ilistdir` instead: https://docs.micropython.org/en/latest/ ... s.ilistdir
Aivar Annamaa
https://thonny.org

bulletmark
Posts: 59
Joined: Mon Mar 29, 2021 1:36 am
Location: Brisbane Australia

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

Post by bulletmark » 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.

MindJuice
Posts: 18
Joined: Tue Apr 14, 2020 6:09 am

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

Post by MindJuice » Sat May 21, 2022 3:59 am

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.

MindJuice
Posts: 18
Joined: Tue Apr 14, 2020 6:09 am

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

Post by MindJuice » Sat May 21, 2022 4:00 am

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.

bulletmark
Posts: 59
Joined: Mon Mar 29, 2021 1:36 am
Location: Brisbane Australia

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

Post by bulletmark » Sat May 21, 2022 4:21 am

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?

MindJuice
Posts: 18
Joined: Tue Apr 14, 2020 6:09 am

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

Post by MindJuice » Sat May 21, 2022 5:57 am

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/`.

Post Reply