I have a python script using Tkinter. In the script I have a numeric variable that the user is able to change the value of by entering the value in an entry box. I presently have the script reading and writing the value to an external .txt file so when the program is exited and restarted the previous value of the variable is retained. I have been unsuccessful in finding a way to store the value inside the script file. Is this possible? If so, could someone point me in the direction of a way to do this so the script would not have to depend on an external file?
Thanks
aframe
1
Newbie Poster
Recommended Answers
Jump to PostThis does the thing, I do not know how good programming style it is to change own code, though:
## My program import sys def between(left,right,s): before,_,a = s.partition(left) a,_,after = a.partition(right) return before,a,after my_value=2160278.18574 print 'Old value',my_value my_value=1.2*my_value print 'New value', my_value my_code=open(sys.argv[0]).read() beginning,value,rest=between('my_value=','\n',my_code) open(sys.argv[0],'w').write(beginning + …
Jump to PostEither externally in a configuration file or database, or in the registry for Windows. There is no other way.
EDIT: or environment variables
Jump to PostI think there is a way to get the path to the .py file.
sys.argv[0]
might work.
I'm not sure though.
Jump to PostResponse to tonyjv: it is a horrible idea to self-modify your code. You can make wonderful messes, though it is kind of fun if you don't have to maintain it. I *think* that the fact the script is byte compiled means you can get away with writing to the .py …
All 16 Replies
TrustyTony
888
pyMod
Team Colleague
Featured Poster
jcao219
18
Posting Pro in Training
aframe
1
Newbie Poster
jcao219
18
Posting Pro in Training
TrustyTony
888
pyMod
Team Colleague
Featured Poster
aframe
1
Newbie Poster
jcao219
18
Posting Pro in Training
aframe
1
Newbie Poster
jcao219
18
Posting Pro in Training
lrh9
95
Posting Whiz in Training
griswolf
304
Veteran Poster
lrh9
commented:
Some good catches. Some bad advice. Otherwise good reply.
+1
lrh9
95
Posting Whiz in Training
TrustyTony
888
pyMod
Team Colleague
Featured Poster
griswolf
304
Veteran Poster
lrh9
95
Posting Whiz in Training
griswolf
304
Veteran Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.