Python Forum
save values permanently in python (perhaps not in a text file)? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: save values permanently in python (perhaps not in a text file)? (/thread-40278.html)



save values permanently in python (perhaps not in a text file)? - flash77 - Jul-04-2023

Hi,
in vb net you can save values ​​permanently with my.settings and load them when loading the form, for example. Is there such a way in python?

Unfortunately, I could only find saving in text files by googling...

I would like to use my python program using an exe file - ideally it should also contain the values ​​that are to be saved permanently.

When the program is ended, the values ​​should be saved and when the exe is started, they should be able to be loaded.

Thanks for the help in advance!!


RE: save values permanently in python (perhaps not in a text file)? - Pedroski55 - Jul-04-2023

How about a .json file?

A .json is actually really a text file. You may open it with a text editor. But a .json file has some tricks up its sleeve!


RE: save values permanently in python (perhaps not in a text file)? - flash77 - Jul-04-2023

Hi,

Thanks a lot for your answer!

I'm looking for a possibility to load/store the values in the project when start/close the project.

If an exe was created from the project, the exe should have the possibility to store/load the values. (In vb.net this is possible with my.settings)


RE: save values permanently in python (perhaps not in a text file)? - flash77 - Jul-04-2023

Hi,
I read that "shelve" would be suitable for that...
Have a nice evening...

https://stackoverflow.com/questions/2960864/how-to-save-all-the-variables-in-the-current-python-session


RE: save values permanently in python (perhaps not in a text file)? - deanhystad - Jul-05-2023

Saving data using shelve, pickle, or json all pretty much work the same way. When you start your program you will read your archived data from a file (in shelve and pickle it will be binary, in json it will be text). When you change something that needs to be persisted, you write the file. Any way you do it you end up with a file that you need to load and update.

I prefer json over pickle or shelve. The file is human readable, and it is really fast. Pickle and shelve allow writing a wider range of types, but I don't often that to be much benefit. Most of the things you save willb e strings, ints or floats, and jsot works fine with thos.

You should post your code and describe what you are trying to save.


RE: save values permanently in python (perhaps not in a text file)? - flash77 - Jul-06-2023

Dear community,

I got it working...

import json
pathA = "C://test//abc//"
pathB = "C://test//def//"

paths = {"pathA": pathA, "pathB": pathB}

# save
with open("paths.json", "w") as write_file:
    json.dump(paths, write_file)

#load
with open("paths.json", "r") as read_file:
    paths = json.load(read_file)

print(paths["pathA"])

print(paths["pathB"])
I can use it to let the user store some paths in the json file and does not have to define it again and again.
I will notice you when I need further advice...


RE: save values permanently in python (perhaps not in a text file)? - buran - Jul-06-2023

There are number of options to store/preserve data. That's called data persistence. Plain file/JSON/XML, tec. may work. You may also want to look into using database. python comes with native support for sqlite3 file-like database


RE: save values permanently in python (perhaps not in a text file)? - deanhystad - Jul-06-2023

For only two strings you could also use the Windows registry. Wouldn't work on Linux though.


RE: save values permanently in python (perhaps not in a text file)? - flash77 - Jul-07-2023

Hi,

thanks for the hint...

Greetings...