Pyinstaller app can't read but can write to files

Question:

I made an app in Python 3 and when I "convert" it to an executable file it can no longer read from files. I assume there is a problem with reading files because writing works perfectly.
I tried both --onedir and --onefile modes, also adding files with --add-data and still nothing.

main.py file:

def write_to_file(content):
    with open('storage.txt', 'w') as f:
        f.write(content)

def read_from_file():
    with open('storage.txt', 'r') as f:
        f.read()

storage.txt file:

some text

My application saves the last state of storage.txt file before "converting" and it can read only the last state, any afterward changes are not readable (storage.txt file content changes)

Asked By: BokiX

||

Answers:

I made this program work with pickle module as it was suggested in the comments by BoadGules. Thank you so much!

"Your program is storing the passwords as Python source code in storage.py. When you run pyinstaller, it converts that source code to bytecode, and that bytecode is what gets wrapped into the .exe. Any subsequent changes your program might make to the source of storage.py has no effect on the way the .exe behaves: your code remains frozen at the original version. That is just how it works. If you want to use pyinstaller then storing data as Python source is not a good solution. Have a look at the pickle module, which is most like what you are doing, or configparser."

Answered By: BokiX
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.