首页 > 编程语言 > 详细

Python初学1

时间:2018-06-05 20:28:32      阅读:176      评论:0      收藏:0      [点我收藏+]

文件读写:

  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

原文:https://www.cnblogs.com/jlyg/p/9141471.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!