Is there any way to save to progress of a Python Program onto an I.P. Address or Harddrive? Like if you enter '5' into a program that multiplies your number by 3 and save it, your saved number would be 'x' when you reopen the file. I need help! :)
#use the current directory or absolute path
f = open("File.txt",'w') #write mode 'w'
answer = 5*3
#make a string, only stings can be added to files
answer = str(answer)
#then open it back up
o = open("File.txt",'r') #read mode 'r'
I have a solution which allows you to use a persistent dictionary between different excecutions of your program, using the with statement (python >= 2.5). Here is an exemple program:
from __future__ import with_statement
from persistent import PersistentDict
if not 'x' in pdict:
pdict['x'] = 0
pdict['x'] += 1
with PersistentDict("persistent.pkl") as pdict:
#every function goes in here
#every variable is renamed self.variable
#so if i was "health" it would be "self.health"
#then you can get rid of all the global this and global that
#because using "self" means that is can be accessed all over the class
n = NorbertQuest()
pickles good for 1 time writing but i would recommend pprint as it allows you too write into a file several times, the only problem is that you start again when you re-open it, search for 'phonebook' in the python forum for an example (i needed help with it)