IO文件读写

*b表示二进制模式访问,但是对于Linux或者Unix系统来说这个模式没有任何意义,因为他们把所有文件都看做二进制文件,包括文本文件

一.三种方法读取文件

 方法1:open

 f=open("D:\hello.txt","r") #已读的方式打开,open只能读文件,不能读文件夹

 fp=f.read()

 print fp

 f.close() #关掉,回收内存

 方法2:with open...as

 with open("D:\hello.txt","r") as fp:

   print fp.read()

 方法3:file  (python2中file和open没什么区别,file是文件对象,open是返回新创建的文件对象的内建函数,在python3中没有file,所以这个方法可忽略)

 f=file("D:\hello.txt","r") 

 fp=f.read()

 print fp

二.用Open写文件

 f=open("D:\hello.txt","wb")

 f.write(" Hello")

 f.close()

原文地址:https://www.cnblogs.com/linbao/p/7723795.html