[Tutor] conventions for establishing and saving default values for variables
Christian Witts
cwitts at compuscan.co.za
Tue Jun 15 15:02:22 CEST 2010
Pete O'Connell wrote:
> Hi I was wondering if anyone could give me some insight as to the best
> way to get and save variables from a user the first time a script is
> opened. For example if the script prompts something like "What is the
> path to the folder?" and the result is held in a variable called
> thePath, what is the best way to have that variable saved for all
> subsequent uses of the script by the same user.
> Pete
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
ConfigParser can read/write config files or using Pickle/cPickle to save
& load your variables or using something like JSON (JavaScript Object
Notation) will do what you want.
For something like just storing a path for future use using ConfigParser
would be perfectly suited to the task.
import ConfigParser
cfg = ConfigParser.ConfigParser()
cfg.read('options.cfg')
if not cfg.sections():
work_path = raw_input('Please enter the workspace path: ')
cfg.add_section('PATH')
cfg.set('PATH', 'workspace', work_path)
f = open('options.cfg', 'w')
cfg.write(f)
f.close()
else:
work_path = cfg.get('PATH', 'workspace')
--
Kind Regards,
Christian Witts
More information about the Tutor
mailing list