1

I need a little bit of help on overwriting python variables. Here's the best example I could give. IF someone opens up my program (in .exe or .py) and this were to come up:

var = 5
var = raw_input("Enter the new variable value: ") ##THE PERSON ENTERS 3
var = 3

Now, the user closes the file/program, next time they open it, they want the program to remember that var = 3, so it overwrote the var = 5 with var = 3. I don't know if this is possible, but if it is, could you let me know?

To Clarify: I want my .py or .exe to remember that a variable was changed, and overwrite the old variable with the new one, so the next time they open the file, the variable is already changed.

3
  • 1
    You want to use file I/O, and explicitly write and read settings like these. Read a decent Python tutorial for beginners first though. Commented Feb 12, 2013 at 18:36
  • 1
    I would suggest you progress a bit further in whatever python tutorial it is you're following. What you're looking to do is something called persistence, and there's a fair bit of basics ahead of you before you hit the parts about persistence. Commented Feb 12, 2013 at 18:38
  • @Jordan Do you want the PROGRAM or any OTHER FILE to keep the change ? If first case, the program will have to change it's own file, I beg it's possible but it will need some skills. If second case, it's persistence Commented Feb 12, 2013 at 20:24

3 Answers 3

4

You'll need to somehow store the value, for example in a file, database, or with some service.

For example, using a simple file:

import io,os
try:
    with io.open('~/.jordan_last_value', 'r', encoding='utf-8') as f:
        var = f.read()
except IOError: # Read failed
    var = raw_input('Enter the new variable value')

    # Save value to disk
    with io.open('~/.jordan_last_value', 'w', encoding='utf-8') as f:
       f.write(var)

print ('Current value is ' + var)

As you can see, this can be somewhat complex, so you may want to refresh your knowledge of file handling. Also, this example code can just store strings.

If you want to be able to store more complex objects, you'll need a way to transform them from and to bytestrings first. Have a look at the json or pickle modules for that.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use pickle module, json or whatever :P

1 Comment

Not a fan of giving people answers that are only literally correct and don't account for context. The context here in my opinion is the asker is missing a lot of programming basics and will just get frustrated jumping ahead to using a module like pickle. The asker's challenge is pedagogical in nature and not technical.
0

Using the pickle module:

import pickle
try: # try to open the file
    with open('jordan_data.pkl', 'r+') as f:
        var = pickle.load(f)
except IOError: # if it doesn't exist, get new input
    var = raw_input("Enter the new variable value: ") # THE PERSON ENTERS 3
    with open('jordan_data.pkl', 'a+') as f: # 'a' will create the file if it doesn't exist
        pickle.dump(var, f) # then, save the file

1 Comment

You don't need close - the with statement does that already. Additionally, you should open the file in binary mode. Also, the renaming of pickle makes it unnecessarily hard to understand the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.