pythonandroidnumpybeeware

Manually deleted numpy arrays saved in .npy files on Galaxy23 with Beeware, yield FileExistsError when trying to run again


My Beeware app generates numpy arrays that I save in .npy format in the Download folder of my Galaxy23. To check if consecutive runs work correctly, before re-running the Beeware app on the phone in developer mode (connected to a PC on windows 11) I manually delete all the .npy files with the phone explorer, but then in the next run I get a crash with a FileExistingError, although all the .npy files are deleted and not seen in the Download folder. Manually empting the Trash folder does not help. The same problem occurrs running the app directly from the phone installation (not connected to the PC).

To be able to re-run the Beeware app correctly on the Galaxy23, I must restart the phone, and then the .npy files are generated again correctly. I use numpy with python 3.8.1 The same problem occurs if I save the numpy arrays in .txt format

When I run the same Beeware app on the Beeware emulator with the same files and the same folder, the problem does not show up.

Can anyone give me a direction on what I am doing wrong? Any suggestion or hint will be much appreciated.

UPDATE: Based on the understanding provided by the answer given by blackapps below, I found a simple workaround that works well for me. The idea is the following: whenever I want to delete a file, say my_file.txt, I first rename it as junk.txt. The MediaStore updates the indexing, so that my_file.txt is not indexed anymore. Then I delete junk.txt, and I can now recreate my_file.txt again. The only file that remains always indexed is junk.txt. I tried it and it seems to do the job both manually and programmatically. The delete function looks as follows:

path="/storage/emulated/0/Documents/my_file.txt"
junk_path="/storage/emulated/0/Documents/junk.txt"

def delete_file():
    try:
        os.rename(path,junk_path)
        os.remove(junk_path)

    except FileNotFoundError:
        pass

Solution

  • The files will have been indexed by the media store.

    There are many sloppy file explorers that only delete the file but not the entry in the media store.

    As long as they are in the media store you cannot create them again.

    Remove the entry in the media store too.

    At reboot the media store scans the whole storage so the entries will go away.