1, open (buffer) file, read the file
1) File Mode
mode | significance |
---|---|
r | Read-only mode |
w | Write Mode |
a | Append mode |
+ | Write (update) mode, used in combination with other modes. R + representatives such as read-write mode, w + also represents the read-write mode (w empty file mode, the actual unreadable) |
b | Binary mode can be used in conjunction with other modes. For example rb represents a binary read-only mode, rb + represent a binary read-write mode, ab append mode representing a binary |
Analysis :
- w only represent write mode, and w + represents the read-write mode, but the actual use, the difference is not large, because the file was cleared.
- r r + mode or open a file, the file itself is a requirement exists, they can not create the file.
- Using w, w +, a, a + mode opens the file, the file can not exist, open () function will automatically create a new file.
- b mode may be added to the other mode for reading and writing files representing a binary content (if b is not specified mode to read and write files character unit; b if specified pattern, in units of bytes to read and write files (The computer does not read the file in units of bits, a byte is 8 bits))
2) open () to open the file and read () to read the file
语法:open(file, mode=‘r’, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
Description : open () function of the third parameter buffering control buffer
-
Is negative, the system default buffer size;
-
Is 0 or False, not buffered;
-
Value of 1 or True, will buffer (recommended, then program execution I / O has better performance);
-
Value is an integer greater than 1, as a buffer size.
(1) create a data.txt file:
(2) read by character
Description :
- B mode without open the file, the file is treated as a character stream, each time a character is read
- open () function total default character set according to the current operating system to read the contents of the file ( see the current operating system character set )
-
Call read (n) method reads n characters
f = open('data.txt', 'r', True) data1 = f.read(1) print(data1) print('-'*30) data2 = f.read(2) print(data2) f.close() # 关闭文件
第 ------------------------------ 一行
analysis:
- data.txt file as GBK character set encoding (the current operating system code is GBK)
- read (1) -> read (2) file pointer
-
Call the read () method all the contents of the file
f = open('data.txt', 'r', True) data = f.read() print(data) f.close()
第一行 第二行 第三行
Analysis : open () function by default character set of the current operating system to read the contents of the file, is apt to cause an exception, resolve as follows.
-
Call open () function to specify the character set to open a file
f = open('data.txt', 'r', True, 'GBK') print(f.read()) f.close()
第一行 第二行 第三行
-
open () function reads the file in binary manner, and to specify the character set byte data recovery (see below)
(3) read byte
Description : Open file b mode, then the file is treated as a binary stream, one byte per read
-
Call Read (n) method reads n bytes
f = open('data.txt', 'rb', True) data1 = f.read(2) print(data1, data1.decode('GBK')) # 字节串,GBK 解码 print('-'*40) data2 = f.read(4) print(data2, data2.decode('GBK')) f.close()
b'\xb5\xda' 第 ---------------------------------------- b'\xd2\xbb\xd0\xd0' 一行
-
Call the read () method all the contents of the file
f = open('data.txt', 'rb', True) data = f.read() print(data) print('-'*50) print(data.decode('GBK')) f.close()
b'\xb5\xda\xd2\xbb\xd0\xd0\r\n\xb5\xda\xb6\xfe\xd0\xd0\r\n\xb5\xda\xc8\xfd\xd0\xd0\r\n' -------------------------------------------------- 第一行 第二行 第三行
Analysis :
- GBK character set encoding each character occupies 2 bytes
- rb mode read files, can be used to handle any file content, such as pictures, music, video and other formats
3) read a text file by line
(1)readline([n])和readlines()
-
readline ([n]): read the contents of a line, if the parameter n specifies only reads n characters in this line
try: f = open('data.txt', 'r', True, 'gbk') while True: line = f.readline() # 读取一行内容 if not line: break print(line, end='') except: print('出现异常') finally: if 'f' in globals(): f.close()
第一行 第二行 第三行
-
readlines (): read all the lines in the file
try: f = open('data.txt', 'r', True, 'gbk') line = f.readlines() # 读取所有行,返回所有行组成的列表 print(line) except: print('出现异常') finally: if 'f' in globals(): f.close()
['第一行\n', '第二行\n', '第三行\n']
(2) use an iterator to read the file
try:
f = open('data.txt', 'r', True, 'gbk')
for line in f: # 文件对象本身可迭代,直接用 for-in 循环即可
print(line, end='')
except:
print('出现异常')
finally:
if 'f' in globals():
f.close()
第一行
第二行
第三行
(3) using the specified row read linecache
Description : linecache module allows to read from a specified row Python source file and stored in the internal cache optimization used. Since the main module is designed to read the source file Python (Python source file in UTF-8 character set is stored), so that the other module file in UTF-8 character set is stored can be read.
# -*- coding:utf-8 -*-
# @Time : 2019/7/5 10:20
# @Author : wangkai
# @File : test.py
import linecache
import random
print(linecache.getline(random.__file__, 3), end='') # 读取random模块的源文件的第3行
print('-'*30)
print(linecache.getline('test.py', 2), end='') # 读取本程序的第2行
integers
------------------------------
# @Time : 2019/7/5 10:20