Monday, 15 July 2013

Save and Overwrite Variables in Python -



Save and Overwrite Variables in Python -

i need little bit of help on overwriting python variables. here's best illustration give. if opens programme (in .exe or .py) , come up:

var = 5 var = raw_input("enter new variable value: ") ##the person enters 3 var = 3

now, user closes file/program, next time open it, want programme remember var = 3, overwrote var = 5 var = 3. don't know if possible, if is, allow me know?

to clarify: want .py or .exe remember variable changed, , overwrite old variable new one, next time open file, variable changed.

you'll need somehow store value, illustration in file, database, or service.

for example, using simple file:

import io,os try: io.open('~/.jordan_last_value', 'r', encoding='utf-8') f: var = f.read() except ioerror: # read failed var = raw_input('enter new variable value') # save value disk io.open('~/.jordan_last_value', 'w', encoding='utf-8') f: f.write(var) print ('current value ' + var)

as can see, can complex, may want refresh knowledge of file handling. also, illustration code can store strings.

if want able store more complex objects, you'll need way transform them , bytestrings first. have @ json or pickle modules that.

python variables memory overwrite

No comments:

Post a Comment