Appending data on an SD card file
I am trying to create a simple data-logger using some very simple sensor inputs as a test. I am using the “SD” and “os” modules to write values to a text file on an SD card on the pymakr board. I can mount it and then open the file to successfully write to the card, however, when I close and then re-open the file when I want to write to again it overwrites my original data.
Is there any way to append the data in the file instead of overwriting?
I can't to keep the file open as I might want to turn the logger off and on over different sessions. I also don’t want to “readall()” my existing data into RAM, append, then re-write the entire data-set as this is ugly, slow and I might have a large amount of data which could crash the system.
Seems like pretty standard functionality to miss out, if this is the case.
I also noticed that there is not a delete file function mentioned in the “os” module which is what the “SD” example code uses, but there is a similar module called “uos” in which you can. Does anyone know why there are two similar “uos” and “os” modules despite being somewhat redundant and why “os” is not in the documentation?
Can anyone clarify this for me please?
Thanks in advance.
Thanks that clears things up a lot!
I didn't know I could list the methods in each module either, useful to know...
That would be fantastic if you could add this sort of extra detail in the documentation, I'm sure that will save me a lot of time!
uos is the micropython version of the os library. When you do import os, it is actually just an alias and it really imports uos. This is for compatibility with existing python code. If you go to the REPL on your device and run
You will see the lists are identical. If we are missing documentation for some of the methods I will add it to my to-do list to add this documentation.
Ah excellent, that's an easy solution!
Thanks again Robert :)
@josh you just have to open the file in append mode:
f = open("the_file", "a")