Fipy, Creating a new file on SD card, but it is protected (no access)!
-
Hello everyone.
I am having some trouble with Micropython, probably it's just me who is doing something wrong. What i want, is to be able to dynamically create a new .txt file on my SD card for the FiPy, and then append data to that file. I am able to create new files without any problems, but when i want to write to them, i get the following error:
Traceback (most recent call last):
File "main.py", line 51, in <module>
File "main.py", line 38, in save_record
OSError: [Errno 13] EACCESI have been using the examples shown here:
https://docs.pycom.io/firmwareapi/pycom/machine/sd/#app
https://docs.pycom.io/firmwareapi/micropython/uos/I am trying to structure my code as a class, here is what i have so far:
class DB(): record_taken = None pending_data = False data_ready = False #Constructor initiates SD card object and mounts the card def __init__(self): sd = machine.SD() os.mount(sd, '/sd') #Method which creates a new record, name is based on the time argument def new_record(self, time): self.time = time uos.mkdir('/sd/'+'record_'+str(time)) #Method to append data to a record, record is again based on time argument def save_record(self, data, time): f = open('/sd/'+'record_'+str(time), 'a') f.write(data) f.close() #Method to save a records data in variable, to be used for publishing etc. def get_record(self, time): self.record_taken = open('/sd/'+'record_'+str(time), 'r') #Method to delete a record, again with the name being decided by the time argument def delete_record(self, time): self.uos.remove('/sd/'+'record_'+str(time)) dibs = DB() dibs.new_record(22) dibs.save_record("testdata", 22)
line 51 is the dibs.save_record(xxx)
line 38 is f = open('/sd/'+'record_'+str(time), 'a')So it would seem that the new file i am creating has somekind of protection on it, that prevents me from opening it. What am i missing?
-
@railmonitor yes. uos.remove() is the method for deleting files.
-
@robert-hh Thanks alot, i was beginning to figure it out myself. A related question is how to delete a file, can it be done without a call to uos.remove()?
Is there something along the lines of this that will work (i tried a delete / remove but these are not known attributes)?:
f = open('/sd/record_1.txt') f.delete()
-
@railmonitor The call to open() with the 'a' mode will create the file if it does not exist yet.
-
@railmonitor said in Fipy, Creating a new file on SD card, but it is protected (no access)!:
OSError: [Errno 13] EACCES
Okay - i think i found the error myself just now - the uos.mkdir() method is for creating new directory, not a file, and ofc i cannot write directly in a directory.
This naturally gives rise to another question: How do i create new files on my SD card, without manually mounting the SD card on my PC and creating the file that way?
-
@railmonitor You try to write to a directory, which you created with mkdir. If you want to a file with that name, the mkdir (or call to new_record) is not required. The call to open() with the 'a' mode will create the files if it does not exist yet.