8000 os.listdir inconsistent · Issue #249 · adafruit/circuitpython · GitHub
[go: up one dir, main page]

Skip to content

os.listdir inconsistent #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
8000
tannewt opened this issue Sep 8, 2017 · 5 comments · Fixed by #932
Closed

os.listdir inconsistent #249

tannewt opened this issue Sep 8, 2017 · 5 comments · Fixed by #932
Labels
Milestone

Comments

@tannewt
Copy link
Member
tannewt commented Sep 8, 2017

After an SD card is mounted, os.listdir returns different results depending on if the path name is given or implicit through the current working directory.

print(os.listdir(os.getcwd()))
print(os.listdir())

Produces:

[b'sd', 'boot_out.txt', '.fseventsd', 'adafruit_sdcard.mpy', 'code.py', '._code.py', 'adafruit_bus_device']
['boot_out.txt', '.fseventsd', 'adafruit_sdcard.mpy', 'code.py', '._code.py', 'adafruit_bus_device']
@RidleyLarsen
Copy link

I'm going to take a look at this.

@RidleyLarsen
Copy link

This is a little outside my comfort zone (which is great!) but I'm reading through the C code. My first intuition is that when the listdir function has to infer the directory, it's not recognizing it as the root directory in the way it would when you explicitly provide it with the root directory (via getcwd()).

@dhalbert
Copy link
Collaborator

It turns out the reason for this has to do with what's mounted where, and how MicroPython was set up to handle mounted filesystems. On the pyboard, the internal flash is mounted as /flash, and an SD card is mounted as /sd:

>>> os.listdir('/')
['flash', 'sd']

On the pyboard, / is not a filesystem: it's only a location for mount points. On the pyboard, you cannot create files or directories in /:

>>> f = open("/foo.txt", 'w')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 19] ENODEV
>>> os.mkdir('/mnt')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 19] ENODEV

The assumption that / is only for mount points is carried through in mp_vfs_lookup_path(), which is called indirectly by os.listdir(). In CircuitPython we mount the the on-board filesystem (CIRCUITPY) on /, not someplace like /flash or /circuitpy. So the fix is probably to make mp_vfs_lookup_path be more flexible.

@deshipu
Copy link
deshipu commented Jun 15, 2018

I don't know if this belongs here, but we also had a user report that if you create a directory called sd and also mount the sdcard as sd, you get "sd" listed twice by listdir.

@dhalbert
Copy link
Collaborator

@deshipu, hmm, that's another bug. It should not let you use a mount point that's the same name as an existing file or dir.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
0