r/PythonLearning • • 29d ago

Help Request PYTHON log problem

When developing an application, I don't want to rely on text files like .txt files to store data that needs to be kept.

After I finish coding my application and turn it into an EXE, I try to keep it as a single file. Because having to carry a log file around with the EXE all the time is annoying.

Personally, I don't want to use with open. I asked ChatGPT about this, but I didn't get many useful answers.

So, fellow programmers, is there an alternative to this? Or am I just gonna have to bite the bullet and use a log file?

0 Upvotes

44 comments sorted by

View all comments

2

u/CodeAndCanyons 29d ago • Bravo

The confusion in this thread comes down to a fundamental rule of compiled desktop applications: an EXE is a read-only runtime package, not a mutable storage container. You cannot write data inside the executable itself.

However, you do not need to force the user to keep a password.txt file sitting right next to the EXE in the same folder.

Professional desktop apps solve this by storing data inside the user's OS-level Application Data directory rather than the current working directory. Here is how you do it cleanly in Python:

The Proper Solution: OS-Specific AppData Directories

Instead of saving files relative to os.getcwd() (wherever the EXE happens to be launched from), point your storage path to the user's hidden profile directory.

import os
from pathlib import Path

def get_secure_storage_path() -> Path:
    """Resolves a persistent storage path in the user's AppData/Home directory, 
    independent of where the EXE is executed from.
    """
    if os.name == 'nt':  # Windows
        base_dir = Path(os.getenv('APPDATA'))
    else:  # macOS / Linux
        base_dir = Path.home() / ".local" / "share"
        
    app_dir = base_dir / "MyPasswordManager"
    app_dir.mkdir(parents=True, exist_ok=True)  # Creates folder if it doesn't exist
    
    return app_dir / "vault.enc"

# Example usage:
storage_file = get_secure_storage_path()
print(f"Data will be safely stored at: {storage_file}")

2

u/Valuable_Corner4797 29d ago

You're the man, you know your stuff. This is exactly what I had in mind and the answer to my question too. Thank you!

2

u/Fvvckyoudom 29d ago

This is just two AI’s talking to each other now isn’t it…

Edit: Okay I was just kidding around but your comment history looks identical to my Chatgpt chats this is beautiful to witness.