Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Fipy, Creating a new file on SD card, but it is protected (no access)!

    Scheduled Pinned Locked Moved MicroPython
    6 Posts 2 Posters 247 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R Offline
      railmonitor
      last edited by

      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] EACCES

      I 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?

      robert-hhR R 2 Replies Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @railmonitor
        last edited by robert-hh

        @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.

        1 Reply Last reply Reply Quote 1
        • R Offline
          railmonitor @railmonitor
          last edited by

          @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?

          robert-hhR 1 Reply Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @railmonitor
            last edited by

            @railmonitor The call to open() with the 'a' mode will create the file if it does not exist yet.

            1 Reply Last reply Reply Quote 1
            • R Offline
              railmonitor
              last edited by

              @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()
              
              robert-hhR 1 Reply Last reply Reply Quote 0
              • robert-hhR Offline
                robert-hh @railmonitor
                last edited by

                @railmonitor yes. uos.remove() is the method for deleting files.

                1 Reply Last reply Reply Quote 0

                Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                With your input, this post could be even better 💗

                Register Login
                • 1 / 1
                • First post
                  Last post

                Pycom on Twitter

                Pycom