Use the standard pickle module. It makes it easy to write to the file and back up most of the standard python objects from the file.
Example of the module:
>>> import pickle >>> data = { ... 'a': [1, 2.0, 3, 4+6j], ... 'b': ("character string", b"byte string"), ... 'c': {None, True, False} ... } >>> # Сохраняем словарь data в файл data.pickle >>> with open('data.pickle', 'wb') as f: ... pickle.dump(data, f) ... # Читаем содержимое файла data.pickle в переменную data_new >>> with open('data.pickle', 'rb') as f: ... data_new = pickle.load(f) ... # Убеждаемся, что в переменной data_new лежит словарь, # идентичный первоначальному >>> print(data_new) {'c': {False, True, None}, 'a': [1, 2.0, 3, (4+6j)], 'b': ('character string', b'byte string')} # Profit!
You can read more about pickle, for example, here: https://pythonworld.ru/moduli/modul-pickle.html