Python初学1
Posted 君凌烟阁
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python初学1相关的知识,希望对你有一定的参考价值。
文件读写:
with open(r‘f:\test\t1.txt‘,‘r‘) as f:
print f.read() 读
f.close() 关闭
with相当于try...catch;
按行读:
with open(r‘f:\test\t1.txt‘,‘r‘) as f:
for l in f.readlines():
print l.strip()
f.close()
写文件:
with open(r‘f:\test\t1.txt‘,‘w‘) as f:
f.write(‘abcde‘)
f.close()
序列化:
import cPickle as pickle 引入包
with open(r‘f:\test\t1.txt‘, ‘wb‘) as f:
dic = 10
pickle.dump(dic, f)
f.close()
with open(r‘f:\test\t1.txt‘, ‘rb‘) as f:
aa = pickle.load(f)
print(aa)
f.close()
以上是关于Python初学1的主要内容,如果未能解决你的问题,请参考以下文章