一.文件写操作 import pickle
三步:(1)开:文件变量 = open(“文件路径文件名”,“wb”)
(2)存:pickle.dump(“待写入的变量”,“文件变量“)
(3)关:文件变量.close()
代码如下:
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> game_data = {"position":"N2 E3","age":18}
>>> save_file = open("a.data","wb")
>>> pickle.dump(game_data,save_file)
>>> save_file.close()
>>> exit()
二.文件读操作 import pickle
三步:(1)开:文件变量 = open(”文件路径文件名“,”rb“)
(2)取:放内容的变量 = pickple.load(文件变量)
(3)关:文件变量.close()
代码如下:
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> load_file = open("a.data","rb")
>>> load_game_data = pickle.load(load_file)
>>> load_file.close()
>>> load_game_data
{'position': 'N2 E3', 'age': 18}