Problem with file system

Dear Sirs,

I have got a multiple problem with file system usage on memory card.
I have created following utility function:

def uprint(*text):
with open(current_work_directory + “Soccer/log/output.txt”, ‘a’) as f:
print(*text, file = f)
print(*text )

It helps all printing outputs redirect to file when OpenMV is detached from computer. Using this utility yields unpredictable result. In some cases I receive expected result, but in some cases print goes in wrong file with unpredictable location. Failure usially shows itself when one of .py files under execution appears spoiled.
It appears that part of source code is replaced by print messages. It seems that Micropython keeps several files open during execution of .py source code and then confuses with them when print instruction is arrived.
There is not reliable method to test what is a real reason because failure is not stable.

Sincerely Azer Babaev

def uprint(*text):
    with open(current_work_directory + "Soccer/log/output.txt", 'a') as f:
        print(*text, file = f)
        print(*text )

I just noticed that code was not send correctly.

Sincerely Azer Babaev

What is current_work_directory ?

If it’s a global variable it should be declared with global, I need to see the full code and how this is called.

import sys
import os
import math
import json

current_work_directory = os.getcwd()
current_work_directory = current_work_directory.replace('\\', '/')

Overall code is too big ( over 5000 lines) and splitted to many modules.
Is this enough information?
Probably it worth to close file after each call of this function, but I am not sure.

Sincerely Azer Babaev

I’ll test it, but you need to add a “/” I think getcwd returns the basename without a trailing slash (which also means the replace(, /) is not needed).

I can’t reproduce this issue, this is what I tested:

import sys
import os
import math
import json

current_work_directory = os.getcwd()

def uprint(*text):
    with open(current_work_directory + "Soccer/log/output.txt", 'a+') as f:
        print(*text, file = f)
        print(*text )


for i in range(0, 10):
    uprint("test")
    uprint("hello", "world")

The file is created normally and nothing is corrupted…Please post a minimal example that shows this issue.

Appearance of failure is not regular therefore it is difficult to catch it if you want to be busy only with finding of it’s evidence.
But in case if you add above part of code into a big project with many files as diagnostic utility and continue your usual work one day you can find that in unexpected place one of files is corrupted.
I am not able to post all my project here because it is too big and it is not likely to be published. This is code with about 5000 lines splitted to 16 source code files which work simultaneously in one cycle. Probably there is a limit for number of files which can be open simultaneously and then printout goes to wrong file.
In case if I actively create and test new parts of code and continue using this utility then at least once a day I see result of problem.
Sincerely Azer Babaev

I understand it’s a random issue, but I can’t begin to debug and fix it unless I can see it happen. Unless you can provide a small example that shows this issue (even if it happens randomly) I can’t do anything about it.