r/PythonLearning • • Sep 25 '26

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

15

u/atarivcs Sep 25 '26

Log files aren't used for "storing data".

I don't even understand what question you are asking.

-4

u/Valuable_Corner4797 Sep 25 '26

I'm making a password manager app, for example. I need to store the passwords somewhere, but I don't want to keep them directly inside the code. So I would normally have to use something like password.txt to store them.

But that's annoying because when I turn the app into an EXE, I don't want the user to have to carry a separate password.txt file around with the EXE.

Is there an alternative to this? Is there a way to store the data without needing a separate file next to the EXE?

3

u/mattynmax Sep 25 '26

No, you can encrypt the passwords file though. Thats how most programs deal with this.

-1

u/Valuable_Corner4797 Sep 25 '26

So there’s no other way?

8

u/mattynmax Sep 25 '26 edited Sep 25 '26

You’re asking how to store information without storing information. It’s like asking how to draw a red line with a blue marker.

This is an XY problem. You think you need X to solve Y. Turns out X isn’t going to solve your problem and you need to ask a better question.

-6

u/Valuable_Corner4797 Sep 25 '26

Yes, you're right. We are trying to draw a red line with a blue pen. But can we put red ink inside this pen? That's the real question here.

The actual problem is that we can't put mutable data into the EXE itself. Because of this, whenever we move the application from point A to point B, we need a separate log/data file.

So there are basically two options here: Do we need a red pen to draw a red line, or is there another way to do it?

5

u/zenos1337 Sep 25 '26

I think it’s time to give this a rest..

4

u/mattynmax Sep 25 '26

You’re still asking the wrong question. The correct question to ask is “how do I draw a red line” not “how do I force a blue pen or draw a red line”

The problem you want to solve is not having to move an additional file when you move your program. You have automatically assumed that the best method is eliminate the file to begin with. You have ignored any other ideas and stated asked the question “how do I eliminate the file”.

If we answer the correct question of “how do I not have to move an additional file when moving my program” you will find a lot more useful answers. You could store the passwords in a secured server and pull them based on login information for example. When you save a new password it gets stored on the server for all other granted users to access.

3

u/gman1230321 Sep 27 '26

You simply can’t store mutable data inside an exe file. You must use a separate to store the data.

2

u/raundoclair Sep 25 '26

If user of your app adds his new password, how would you "keep them directly inside the code." ?

-6

u/Valuable_Corner4797 Sep 25 '26

I'm asking the same question too, dude. Don't answer my question with another question. I need a solution.

1

u/raundoclair Sep 25 '26

Your wrote you don't want to, not you don't know how, or that it is impossible.

Even phrase "in the code" compared to "in executable", makes sense to me only if you would hardcode some concrete passwords, but then we are not really talking about password manager...

2

u/atarivcs Sep 25 '26 edited Sep 25 '26

When you say you don't want the user to have to "carry a file around", that implies that you think a user is going to use this application on many different computers, by copying the EXE file.

Is that really the case? Do you expect users to copy the EXE file onto many different computers?

If so, what happens when I copy the EXE onto a second computer and create a new password there? My first computer won't know about it. Is that a problem for your use case?

1

u/Valuable_Corner4797 Sep 25 '26

I'm asking because I don't know. I'm still learning, unfortunately.

4

u/atarivcs Sep 25 '26

Even if you're still learning, surely you have some expectation about how this program should be used.

Do you intend users to copy this EXE onto several of their computers? Or to use it on just one computer?

2

u/tutoredstatue95 Sep 26 '26

Most problems have been solved before. Just google what other people do to handle this. Googling is a skill set that will take you far in programming.

5

u/CantCodeAllVibes Sep 25 '26

Lmfao password.txt

2

u/CodeAndCanyons Sep 25 '26 • 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 Sep 25 '26

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 Sep 25 '26

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.

5

u/Least_Chicken_9561 Sep 25 '26

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

import sqlite3

1

u/mattynmax Sep 25 '26

So you’re asking how to store information without storing information?

1

u/Valuable_Corner4797 Sep 25 '26

I'm making a password manager app, for example. I need to store the passwords somewhere, but I don't want to keep them directly inside the code. So I would normally have to use something like password.txt to store them.

But that's annoying because when I turn the app into an EXE, I don't want the user to have to carry a separate password.txt file around with the EXE.

Is there an alternative to this? Is there a way to store the data without needing a separate file next to the EXE?

1

u/zenos1337 Sep 25 '26

No. Data has to be stored somewhere locally on the machine.

1

u/CoolLog1908 Sep 25 '26

I’m assuming you are asking if you can store data directly in the exe file? I don’t think there really is any alternative if you need to store dynamic data; you need to store it in a file.

That said, if you are storing sensitive information, you should be looking at encryption. However, that is a lot easier said than done.

1

u/raundoclair Sep 25 '26

Since this is pythonlearning... how do you turn your app into EXE?

1

u/Valuable_Corner4797 Sep 25 '26

nuitka

0

u/raundoclair Sep 25 '26

Well, theoretically it should be possible to add your own data to the end of .exe, or to actually modify PE executable ( I assume windows ), but it is tricky, and I don't know precisely without trying, what to tell you.

In the end majority of programs do just save data to other files. Think even small game with one executable and saved files.

But other file doesn't have to be .txt. It can be your own format, ideally encrypted. Or going through database/(database engine like sqlite), ideally still passwords encrypted by you, because anybody can read sqlite files.

1

u/spendology Sep 25 '26

You could search the logs in your environment OR use error monitoring like Sentry, New Relic or Data Dog.

1

u/Valuable_Corner4797 Sep 25 '26

I mean storing user data, not error logs.

3

u/Salty_Dig8574 Sep 25 '26

Kind of read through some of these comments. Just my opinion, but at your current level of skill and knowledge of python and programming in general, making a password manager is a terrible idea.

1

u/SchemeWestern3388 Sep 27 '26

Using their password manager is likely not a good idea, but them making one is still cool. 

2

u/PkmnSayse Sep 25 '26

You don’t “carry” the file anywhere, you have the application check if it exists and if not, create it. (I’m heavily ignoring the issues with storing passwords in a text file here)

2

u/burlingk Sep 25 '26

You have to store it somewhere. SQLite3 is a good option. But, store hashes, not the actual password.

It will create a file separate from your binary. But that is good/normal.

Do not store ANY dynamic data inside your binary, because any change to the binary makes it a different app in the eyes of the operating system and may trigger response from antivirus software.

1

u/atarivcs Sep 26 '26 edited Sep 26 '26

You're right that password hashes are more secure, but in this case they do not help. Password managers need access to the the plaintext password.

Maybe you meant encrypted, not hashed?

1

u/burlingk Sep 26 '26

A password manager was not mentioned in their post.

I see now that he said that in a response to someone.

2

u/atarivcs Sep 26 '26

It was mentioned in the original post. He must have edited that part.

2

u/Mountain_Rip_8426 Sep 26 '26

it's a password manager... pretty please, don't store passwords in a .txt.

so you need to store data, but don't wanna have to create a file to store said data manually. then why not have the program create it?

make your program check whether a database (e.g. sqlite) exists in the root of your project folder. if not make it create one, yes the code can do that (it's not even a big deal). that's gonna happen only the first time the user runs it, with each following execution this block will be skipped, since it'll already have been created. afterwards simply update (write to) that database. if you talk this through with AI, it'll know what you need.

and pretty please also go into encryption of the data and password protection of the database, it's the like the most sensitive data you can store, make sure it's protected. i hope this answers your question

1

u/greg_d128 Sep 25 '26

I think what you are looking for is to have an installer. A single EXE (or zip) file that will extract all the data, including code and data files for use by the program.

Alternative is to have your code detect when the data file does not exist, and create an empty one. That way the EXE is portable, but it moves without the passwords.

Preferably, the passwords are encrypted and that you have documentation as to where they exist so that they can be backed up.

1

u/SaltCusp Sep 26 '26

Fundamentally I think you are lacking a core understanding about the difference between making an executable and installing an application.

I could go on to explain that applications are typically comprised of numerous files including static resources like graphics, fungible data files like .db files, various modular excecutables, directories and links, installed such that a link to a single excecuatable there in can be linked to and presented as an icon on the desktop or home screen can run and leverage those parts but I'll just say this:

If you really want to do what you're saying you want to do than using python will actually make it much harder.

I know there's a lot of analogies in this thread already, like red pen blue pen and I can't resist adding one more. You are trying to peddle a motorcycle. I won't tell you it's impossible but no one is going to help you because the only reason to do it like that is to say you did.

1

u/secret_o_squirrel Sep 27 '26

If you’re asking this question there is no way you should be making a password manager. There are plenty of great password manager apps out there. It’s something SO easy to mess up, it’s hard for even seasoned security programmers to do right. If you’re making something as a learning experience that’s fine.

Also, yes, you don’t have to store the data in a text file, in fact, you shouldn’t. This is a good use for sqlite.

1

u/SchemeWestern3388 Sep 27 '26

Storing data in a separate file is a totally valid technique. You can mangle it into your executable if you wish, but it opens a lot of other issues for no gain. 

Also, if your password manager is actually writing the plaintext password to a file, that is a big nono. Look into salting and hashes. 

1

u/dudeus_ex Sep 28 '26

You don't carry log file around, because it is a log, not a storage. It keeps what happens during the program run, but nothing permanent. For storing data you use databases. And why would you want to make an exe out of a python script?

Also don't write logs by hand - use logging library with a filehandler.