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()

  

  

猜你喜欢

转载自www.cnblogs.com/jlyg/p/9141471.html