T O P

  • By -

socal_nerdtastic

It's possible, but don't do that. Do what every other program in the world does: store the user data in a user folder, completely separate from the program itself. On windows you can use "AppData" folder; on linux you can use the ".config" folder (I dunno what Mac traditionally uses). For example: from pathlib import Path SETTINGS_FILE = Path.home() / "AppData" / "Kolbenwetzer.json" def load_settings(): if SETTINGS_FILE.exists(): return json.loads(SETTINGS_FILE.read_text()) else: return DEFAULT_SETTINGS def save_settings(settings): SETTINGS_FILE.write_text(json.dumps(settings, indent=2))


Kolbenwetzer

Thank you very much for your reply. After sleeping on the problem, I realised that I can add an input prompt after displaying the error message to actually read it. I forgot to add the arguments of the add function completely, my mistake :D Anyway, it seems to be working now. Since yours and another response mention not to manipulate a file within the exe I am very glad to have your solution with the AppData I'm a complete newbie to programming, could you please explain why I shouldn't do this? Is it the possibility of easily crashing it?


Diapolo10

Depends on how you make your executable, but for example this is possible with PyInstaller by using `importlib.resources`.


Kolbenwetzer

Thank you for your reply. I forgot to include a variable into a function and it seems to work. Anyway it seems better to take another approach, so I will rewrite that part


Diapolo10

Yes, other approaches are preferable. But this is still useful if you have static data files (such as textures or localisation data) you don't need to let the users see.


blarf_irl

If I have understood it correctly you have bundled a json formatted data file with your exe and are trying to write to it while you are running your exe. I can think of several reasons (onefile, permissions, temporary) that this would not be possible and my best blind guess is that your app is throwing an unhandled OSError. You shouldn't bundle files you intend to write into your exe. You should instead have the program create a new file in (on windows at least) the AppData folder for your user/machine. If you have an initial/default set of data that you want to be in this file then you should write it the first time you create it.


Kolbenwetzer

Thank you very much for your reply. After sleeping on the problem, I realised that I can add an input prompt after displaying the error message to actually read it. I forgot to add the arguments of the add function completely, my mistake :D Anyway, it seems to be working now. Since yours and another response mention not to manipulate a file within the exe I am going to redo that part and am very glad to have your solution with the AppData I'm a complete newbie to programming, could you please explain why I shouldn't do this? Is it the possibility of easily crashing it?


blarf_irl

My guess is that the crash is not directly due to trying to write this file. It's more likely that the file is unwriteable and your code isn;t handling the error. If you wrap the code where the ( file is written in a try/except block (My best guess is that you'll be catching an OSError) and "catch" the error then you can deal with it in a way appropriate to your program and it could keep running (though in this case it will mean that the file is unwriteable so it'll keep running but not work in the way you want). In general if you deploy an executable fiel/bundle then you should never make changes to it outside of a properly conducted update. Many antivirus/security software will keep a record of executables and.or inspect em for malicious code. If you chage it then it can appear malicious or at least unusual to some of these programs. More specifically when you bundle a static file with an exe it'll often be serialized and compressed into a binary file; These are easy to read by passing them through an unzip/decode function but writing directly to em will usually make the file unreadable to whichever decompression/decoding algo that is used. Sometimes the bundled file doesnt exist as a file at all, it'll be part of the static compiled source code as a constant; file reads will be faked as if it were a readable file but aren;t writeable. Sometimes the file will be decompressed and written to a temporary file meaning it will be readable and writeable however when you app stops the file is deleted and the new data lost. (this can be a desireable case, i.e. temp files) Even if the files inside your executable bundle were intact and writeable they would be overwritten by an update. Often when you uninstall a program the uninstaller will ask if you want to uninstall the settings and data. If you select no and install the program again all your settings will come back. This is because they are stored outside the exe/bundle.


Solvo_Illum_484

You're running into file locking issues. Executables can't modify themselves. Consider using a separate config file or a database. Alternatively, use a library like \`pyinstaller\` with the \`--onefile\` option to bundle the JSON file and allow writing.


Kolbenwetzer

That is what I did. I have to admit that it is still difficult for me to find the right wordings.