存储器
python中可以把对象存到文件当中,成为永久性的对象。
01 | #!/usr/bin/python |
02 | # Filename: pickling.py |
03 |
04 | import cPickle as p |
05 | #import pickle as p |
06 |
07 | shoplistfile = 'shoplist.data' |
08 | # The name of the file where will be store the object |
09 |
10 | shoplist = ['apple', 'mango', 'carrot'] |
11 |
12 | # Write to the file |
13 | f = file(shoplistfile, 'w') |
14 | p.dump(shoplist, f) # dump the object to a file |
15 | f.close() |
16 |
17 | del shoplist #remove the shoplist |
18 |
19 | # Read back from the storage |
20 | f = file(shoplistfile) |
21 | storedlist = p.load(f) |
22 | print storedlist |
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。