Forum Archive

Save variable to use it in next script execution

nealtz

I search for a way to save a variable so that I can recall it in the next execution of the script. I want to use it like a counter. Or is there a way to interact with some kind of database? Any hints?

ccc

Read/write to a file, json, pickle, marshal, sqlite3, there are a million ways to do this but...

import contextlib, shelve
file_name = 'my_shelve'  # Pythonista will create '.bak', '.dat', and '.dir' files
with contextlib.closing(shelve.open(file_name)) as d:
    print(d.setdefault('counter', 0))
    d['counter'] += 1
henryiii

Or pretend it is a password and use keychain.

ccc

http://omz-forums.appspot.com/pythonista/post/5878444697059328

nealtz

Thanks for the information!