6

What is the preferred/ usual way of storing data that is entered by the user when running a Python script, if I need the data again the next time the script runs?

For example, my script performs calculations based on what the user enters and then when the user runs the script again, it fetches the result from the last run.

For now, I write the data to a text file and read it from there. I don't think that I would need to store very large records ( less than 100, I'd say).

I am targeting Windows and Linux users both with this script, so a cross platform solution would be good. My only apprehension with using a text file is that I feel it might not be the best and the usual way of doing it.

So my question is, if you ever need to store some data for your script, how do you do it?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user225312
  • 126,773
  • 69
  • 172
  • 181

4 Answers4

11

you could use a slite database or a CSV file. They are both very easy to work with but lend themselves to rows with the same type of information.

The best option might be shelve module

import shelve

shelf = shelve.open(filename)
shelf['key1'] = value1
shelf['key2'] = value2

shelf.close()
 # next run
shelf.open(filename)

value1 = shelf['key1']
#etc
aaronasterling
  • 68,820
  • 20
  • 127
  • 125
  • 1
    @aaronsterling shelve module by default creates file in current directory. what's the best practice of saving application data in python. I mean inside the module or user's directory? – himanshu219 Jan 31 '19 at 14:37
6

For 100 lines, plain text is fine with either the standard ConfigParser or csv modules.

Assuming your data structure is simple, text affords opportunities (e.g. grep, vi, notepad) that more complex formats preclude.

msw
  • 42,753
  • 9
  • 87
  • 112
6

For small amounts of data, Python's pickle module is great for stashing away data you want easy access to later--just pickle the data objects from memory and write to a (hidden) file in the user's home folder (good for Linux etc.) or Application Data (on Windows).

Of, as @aaronnasterling mentioned, a sqlite3 file-based database is small, fast and easy that it's no wonder that so many popular programs like Firefox and Pidgin use it.

ewall
  • 27,179
  • 15
  • 70
  • 84
  • Oh yes, sqlite is perfect for me since it doesn't require any installation. – user225312 Sep 03 '10 at 03:46
  • +1 for mentioning [where to save the file](http://stackoverflow.com/questions/1084697/how-do-i-store-desktop-application-data-in-a-cross-platform-way-for-python/1088459#1088459). – mgalgs Jul 05 '11 at 17:35
3

Since you only need the last result, just store the result in a file.

Example

write('something', wb)

It will only store the last result. Then when you re-run the script, do a open and read the previous result.

Community
  • 1
  • 1