python基础6--文件操作--对象的读写
代码
# file's name : obj_w_r.py
# import cPickle as pk
import pickle as pk
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def sayhi(self):
print "Hello everyone, my name is", self.name, ", I'm", self.age, "years old"
p = Person("nami", 24)
filename = "c:/nami.data"
# save person to nami.data
f = file(filename, "w")
pk.dump(p, f)
f.close()
# read person from nami.data
f = file(filename)
p = pk.load(f)
# call person's sayhi method
p.sayhi()
输出结果:
Hello everyone, my name is nami , I'm 24 years old
以上是 python基础6--文件操作--对象的读写 的全部内容, 来源链接: utcz.com/z/388673.html