一打开文件
二操作文件
三关闭文件
open(文件,模式,编码),打开文件----->0101010(以二进制的方式打开)------>编码(open默认utf-8编码)------>显示
- r ,只读模式【默认】
- w,只写模式【不可读;不存在则创建;存在则清空内容;】
- x, 只写模式【不可读;不存在则创建,存在则报错】
- a, 追加模式【可读; 不存在则创建;存在则只追加内容;】
"+" 表示可以同时读写某个文件
- r+, 读写【可读,可写】指针在起始位置
- w+,写读【可读,可写】指针在起始位置
- x+ ,写读【可读,可写】指针在起始位置
- a+, 写读【可读,可写】读取的时候指针会在最后这样就是追加文件内容,如果此时读取文件内容,那么将显示空
"b"表示以字节的方式操作,注意b是操作字节这里就不需要编码了。打开文件----------->010101010--------->显示。这里没有编码过程。要是想显示出来就的将二进制转成特定的编码才可以。str("二进制对象001010101",encoding="utf-8"),这样就对二进制进行了编码翻译我们就可以正常读取了。
- rb 或 r+b
- wb 或 w+b
- xb 或 w+b
- ab 或 a+b
注:以b方式打开时,读取到的内容是字节类型,写入时也需要提供字节类型.bytes("你好",encoding="utf-8")将字符串转换成二进制
r/w/x/a =>字符串格式
rb/wb/xb/ab => 字节类型(二进制)soket
中文占用3个字符,
查看指针
f.open('xxxx.log','a',encoding='utf-8')
f.tall()
调整指针位置
f.seek(num)
文件方法介绍:
1 class file(object): 2 """ 3 file(name[, mode[, buffering]]) -> file object 4 5 Open a file. The mode can be 'r', 'w' or 'a' for reading (default), 6 writing or appending. The file will be created if it doesn't exist 7 when opened for writing or appending; it will be truncated when 8 opened for writing. Add a 'b' to the mode for binary files. 9 Add a '+' to the mode to allow simultaneous reading and writing. 10 If the buffering argument is given, 0 means unbuffered, 1 means line 11 buffered, and larger numbers specify the buffer size. The preferred way 12 to open a file is with the builtin open() function. 13 Add a 'U' to mode to open the file for input with universal newline 14 support. Any line ending in the input file will be seen as a ' ' 15 in Python. Also, a file so opened gains the attribute 'newlines'; 16 the value for this attribute is one of None (no newline read yet), 17 ' ', ' ', ' ' or a tuple containing all the newline types seen. 18 19 'U' cannot be combined with 'w' or '+' mode. 20 """ 21 def close(self): # real signature unknown; restored from __doc__ 22 关闭文件 23 pass 24 25 def fileno(self): # real signature unknown; restored from __doc__ 26 文件描述符,这个在soket io中会有用,检测文本或soket是否有变化就是靠这个 27 return 0 28 29 def flush(self): # real signature unknown; restored from __doc__ 30 将内存数据刷新到硬盘 31 pass 32 33 def isatty(self): # real signature unknown; restored from __doc__ 34 判断文件是否是同意tty设备 35 return False 36 37 def next(self): # real signature unknown; restored from __doc__ 38 获取下一行数据,不存在,则报错 39 pass 40 41 def read(self, size=None): # real signature unknown; restored from __doc__ 42 读取数据,可以选择读取的字符串或字节大小 43 pass 44 45 def readinto(self): # real signature unknown; restored from __doc__ 46 读取到缓冲区,不要用,将被遗弃 47 pass 48 49 def readline(self, size=None): # real signature unknown; restored from __doc__ 50 仅读取一行数据,节约内存资源 51 pass 52 53 def readlines(self, size=None): # real signature unknown; restored from __doc__ 54 读取所有数据,并根据换行保存值列表 55 return [] 56 57 def seek(self, offset, whence=None): # real signature unknown; restored from __doc__ 58 指定文件中指针位置 59 pass 60 61 def tell(self): # real signature unknown; restored from __doc__ 62 获取指针位置 63 pass 64 65 def truncate(self, size=None): # real signature unknown; restored from __doc__ 66 截取数据,截取指针位置之前的数据 67 pass 68 69 def write(self, p_str): # real signature unknown; restored from __doc__ 70 写入数据 71 pass 72 73 def writelines(self, sequence_of_strings): # real signature unknown; restored from __doc__ 74 将一个字符串列表写入文件 75 pass 76 77 def xreadlines(self): # real signature unknown; restored from __doc__ 78 可用于逐行读取文件,非全部 79 pass
提示:x.read()一次性读取所有数据到内存,如果对象很大那么这样做很不好
下面的方法也是一行一行读取
a=f.open("xxx.log",'r')
for line in a:
print line
with 方法读写文件
with open('xxx.log','r') as f: 等同于 a=f.open("xxx.log",'r')
f.read()
使用with 不需要写close,它会帮你自己动关闭
在python2.6以后with同时可以打开2个文件
with open('xxx.log','r') as f,with open('xxx.log1','r') as f1: