I am writing a program in Python where a user can input information that is saved - even if the program is terminated. How do I implement this?
1 Answer
Assuming you want the user to input a string of data, you can use raw_input() to get it, and then send it to a file using pickle library, as mentioned in comments above.
To use pickle, you should before open a file in writing mode and then use this file to dump into it your object containing the string.
A secure way to open a file is to use a with statement, it will be closed at the end of the statement.
import pickle
myData = raw_input('What is your name ? ')
with open("name.p", "wb" ) as myFile:
pickle.dump(myData, myFile)
Later, you can get back the object by opening the pickle file in read mode and store its content in a variable.
import pickle
with open("name.p", "rb" ) as myFile:
myData = pickle.load(myFile)
pickle. It allows you to easily save ('serialize') data of any Python type and later reimport those.jsonmodule works quite well for many purposes.picklehas security issues (see here) and I would not advice it unless I'm really sure what it's being used for.