Python Forum
save values permanently in python (perhaps not in a text file)?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
save values permanently in python (perhaps not in a text file)?
#1
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!!
Reply
#2
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!
lothar likes this post
Reply
#3
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)
Reply
#4
Hi,
I read that "shelve" would be suitable for that...
Have a nice evening...

https://stackoverflow.com/questions/2960...on-session
Reply
#5
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.
Reply
#6
Smile 
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...
Reply
#7
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
For only two strings you could also use the Windows registry. Wouldn't work on Linux though.
Reply
#9
Hi,

thanks for the hint...

Greetings...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 337 Jan-24-2024, 06:28 PM
Last Post: frohr
  how to save to multiple locations during save cubangt 1 562 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  Replace a text/word in docx file using Python Devan 4 3,467 Oct-17-2023, 06:03 PM
Last Post: Devan
  Save and Close Excel File avd88 0 3,084 Feb-20-2023, 07:19 PM
Last Post: avd88
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,134 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Save multiple Parts of Bytearray to File ? lastyle 1 962 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,696 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Overwrite values in XML file with values from another XML file Paqqno 5 3,338 Apr-01-2022, 11:33 PM
Last Post: Larz60+
  How to split file by same values from column from imported CSV file? Paqqno 5 2,806 Mar-24-2022, 05:25 PM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,018 Feb-11-2022, 12:38 AM
Last Post: atomxkai

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020