REPL limitations? SD mount works on board but not via REPL
Are there limitations to what can be run via REPL vs actually uploaded to the device?
I'm running Visual Studio Code with Pymakr 1.1.6 connecting to a LoPy4 (1.20.2.rc6) on a Pysense board via USB to my Win10 machine.
I've got a fairly basic SD card script that fails on
os.mount(sd, '/sd')when run through REPL, but it works when uploaded to the board.
I normally click the "> Run" button on the VSC taskbar to "run the current file".
Am I doing something wrong? Is there a better way? Any ideas of why this SD card isn't mounting via REPL (it's a 32GB level 4 card)?
from machine import SD import os print('Start') gc.enable() sd = SD() print('Code point 1') os.mount(sd, '/sd') print('Code point 2') f = open('/sd/test.txt', 'a') f.write('Testing SD card write operations') f.write('\n') f.close()
>>> >>> Start Code point 1 Traceback (most recent call last): File "<stdin>", line 8, in <module> OSError: [Errno 1] EPERM >
railmonitor last edited by railmonitor
Great that it worked out. For anyone seeing this thread, but not finding what they need in the pycom documentation, there are also the possibility to delete items on the sd card using the command:
path could be something like:
@jcaron @railmonitor I've done some more research, and because I didn't find this info anywhere else, here's what I found (please correct me if you see anything wrong).
If you run
os.mount(sd, '/sd')when it is already mounted you get this error:
OSError: [Errno 1] EPERM
When there is no SD card physically in the slot, the mount doesn't throw an error, but when you try and open a file
f = open('/sd/file.txt', 'a')you'll get error:
OSError: [Errno 19] ENODEV
Trying to unmount: using the command specified in the pycom docs:
uos.unmount()the error is
AttributeError: 'module' object has no attribute 'unmount'
Obviously this is because
unmountisn't right, but the micropython docs set me straight - it should be
umount('/sd')works as expected. I will submit a pull request on the pycom docs now to fix them.
If you try and
umountwhen there's nothing mounted you'll get this error:
OSError: [Errno 22] EINVAL
So, a try/catch around the mount to see if it's already mounted, a try/catch around the file open to see if there's an SD card, and a try/catch around any unmounts just in case there's nothing mounted there will catch all of these scenarios.
Heaps of extra info on this reply I know, hopefully it helps with anyone in the future searching.
@mk what error are you getting? Do you have any open files on that fs?
railmonitor last edited by
@mk I would guess the same path as when mounting.
@jcaron great point! You are correct, have just wrapped the mount in a try catch and then moved on to the the file write either way and it works fine. I didn't think to do that!
It did occur to me the other day that it might need unmounting first, but the unmount was failing too - the unmount docs are a bit vague, so I think I was doing it wrong...
from: pycom uos docs
uos.mount(block_device, mount_point, * , readonly=False) Mounts a block device (like an SD object) in the specified mount point. Example: os.mount(sd, '/sd') uos.unmount(path) Unmounts a previously mounted block device from the given path.
and they both failed with errors. Do you know what
@mk Is the SD card not already mounted?