码迷,mamicode.com
首页 > 编程语言 > 详细

Python初学1

时间:2018-06-05 21:19:37      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:序列   lin   for   nes   文件   rip   读写   初学   txt   

文件读写:

  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

标签:序列   lin   for   nes   文件   rip   读写   初学   txt   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!