opening existing file on /flash for appending

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
smhodge
Posts: 86
Joined: Tue Jan 22, 2019 2:16 am
Location: Kirkland, WA, USA

opening existing file on /flash for appending

Post by smhodge » Tue Sep 01, 2020 2:18 am

I am trying to open an existing file stored on /flash for appending & reading. It is a binary data file created with a prior run of my code. I can do it just fine from the REPL with the following code:
---------------------------
filePath = '/flash/cos.log'
file = open(filePath,'a+b')
from uos import stat
s = stat(filePath)
print ("File", filePath, "opened for logging, length =", s[6], "bytes")
---------------------------
output:
File /flash/cos.log opened for logging, length = 2652 bytes
Free disk space = 60928 bytes
---------------------------
However when I run the exact same lines from within my code I get a length of 0 bytes. It appears that it is not opening it to append and instead is opening a new file and the contents of the old file of the same name are lost.

Any ideas why it works from the REPL but not from my code? This is on a v1.1 pyboard, with v1.12 firmware.

IHOXOHI
Posts: 119
Joined: Sat Apr 25, 2020 7:31 am

Re: opening existing file on /flash for appending

Post by IHOXOHI » Tue Sep 01, 2020 8:01 am

Hi Smhodge,

Maybe I'm wrong but the usual procedure for save data contain 3 importants parts:
-You have the first one with "file = open(file-path, 'a+b')", one time.
-But after you have to use "file.write(data)" each time that you write data.
-and finally for save data, "file.close()", one time.

So, usually you have to use a button in a special loop for the last command.

I have writed a solution on the subject "pyboard-d and datalogging" in this forum.

smhodge
Posts: 86
Joined: Tue Jan 22, 2019 2:16 am
Location: Kirkland, WA, USA

Re: opening existing file on /flash for appending

Post by smhodge » Tue Sep 01, 2020 2:50 pm

I don't have a problem writing the data. If I let my code run after it has opened the now new file it writes new data just fine. The problem is I can't get to first base with the "open append" function: it just trashes the old existing file and starts a new one. BUT if i run the same code in the REPL (on the same file) it works just fine; it does the append as desired.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: opening existing file on /flash for appending

Post by Roberthh » Tue Sep 01, 2020 3:07 pm

Did you close the file after writing with f.close()?
I just tested that on a pyboard 1.1 with the following code snippet:

Code: Select all

with open("test.bin", "a+b") as f:
    f.write(bytearray(500))
Note that the with.. statement closes the file when it's body is left. That ensures that the data is actually written to the file.

smhodge
Posts: 86
Joined: Tue Jan 22, 2019 2:16 am
Location: Kirkland, WA, USA

Re: opening existing file on /flash for appending

Post by smhodge » Tue Sep 01, 2020 4:41 pm

I just did some more tests. I does not matter if I use close() or not, or if I use mode='a+b', 'w+b' or 'r+b'. In all cases it finds the file on /flash just fine but it opens it with a file length of 0 and thus new writes to it over-write the old contents. BUT, in all cases, the same code lines work just fine from the REPL -- they open the file with the correct (non-zero) length; that's what is frustrating because I can't use the REPL to debug it.

After I write data to the file, and close it (or not), it lists just fine in rshell, has the expected length, and I can copy it with rshell to the computer and look at it with a hex editor (it is a binary file) and it is correct. The REPL works ok but the exact same code lines when run as actual code do not-- they always give a length of 0. The code lines are part of the constructor of a "log" object, but I wouldn't think that would make a difference.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: opening existing file on /flash for appending

Post by Roberthh » Tue Sep 01, 2020 4:57 pm

Can you post the code that behaves not well?
And please explain how you execute that code.

Post Reply