Python 完美诠释高内聚概念的 IO 流 API 体系结构(全网最详细) (3)

调用 open( ) 函数中使用文本模式时返回的是 TextIO 对象,相比较父类,多了几个特定于文本操作的属性。

class TextIO(IO[str]): # 缓存信息 @abstractproperty def buffer(self) -> BinaryIO: pass # 设置编码 @abstractproperty def encoding(self) -> str: pass # 设备错误处理方案 @abstractproperty def errors(self) -> Optional[str]: pass # 设置行缓存 @abstractproperty def line_buffering(self) -> bool: pass # 换行符的设置方案 @abstractproperty def newlines(self) -> Any: pass 3.1 文本文件读操作

基本操作

file = open("guo_ke.txt", mode='r') print("读写模式:", file.mode) print("文件名:", file.name) print("文件是否关闭:", file.closed) print("文件描述符号:", file.fileno()) print("文件是否可读", file.readable()) print("是否是标准输入流:", file.isatty()) print("文件是否可写:", file.writable()) print("缓存方案", file.buffer) print("文件默认编码:", file.encoding) print("编程错误处理方案", file.errors) print("是否设置行缓存", file.line_buffering) print("换行符的设置方案", file.newlines) ''' 输出结果 读写模式: r 文件名: guo_ke.txt 文件是否关闭: False 文件描述符号: 3 文件是否可读 True 是否是标准输入流: False 文件是否可写: False 缓存方案 <_io.BufferedReader> 文件默认编码: cp936 编程错误处理方案 strict 是否设置行缓存 False 换行符的设置方案 None '''

cp936 指的是系统的第 936 号编码方案,即 GBK 编码。

多样化的读方法:

无论是读还是写时,需要理解一个文件指针(光标)的概念,也可理解为文件位置。读或写时,只能从当前位置向前移动。

提前准备好一个文本文件,在文件中写入如下内容

You hide in my heart deeply. Happiness! There is only you and I together time... With you just I don't want to give anyone the chance. Honey, can you marry me, I'll marry you! Don't know love you count is a close reason?

read( ) 方法的使用

file = open("guo_ke.txt", "r") print("----------读取所有内容--------------") res = file.read() print(res) print("----------读取部分内容--------------") # 重新回到文件头 file.seek(0) res = file.read(100) print(res) # 关闭文件资源 file.close() ''' 输出结果 ----------读取所有内容-------------- You hide in my heart deeply. Happiness! There is only you and I together time... With you just I don't want to give anyone the chance. Honey, can you marry me, I'll marry you! Don't know love you count is a close reason? ----------读取部分内容-------------- You hide in my heart deeply. Happiness! There is only you and I together time... With you just I don '''

这里有一个细节要注意:

第一次读取完所有文件内容后,读取位置已经移到了文件尾部。继续读取时是不能读到数据的。

可通过 seek( ) 方法,把光标移到文件头部。

readline( ) 方法的使用

file = open("guo_ke.txt", "r") print("---------读取一行--------") res = file.readline() print("数据长度:", len(res)) print(res) print("-----------限制内容-------------") res = file.readline(10) print("数据长度:", len(res)) print(res) print("-----------以行为单位读取所有数据-------------") #回到文件头部位置 file.seek(0) while True: res = file.readline() print(res) if res == "": break file.close() ''' 输出结果 ---------读取一行-------- 数据长度: 29 You hide in my heart deeply. -----------限制内容------------- 数据长度: 10 Happiness! -----------以行为单位读取所有数据------------- You hide in my heart deeply. Happiness! There is only you and I together time... With you just I don't want to give anyone the chance. Honey, can you marry me, I'll marry you! Don't know love you count is a close reason? '''

一行一行读取所有内容时,输出时会在行与行之间产生一个空行。原因是行结束符号 'n' 会被当成一个空行输出。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzfysf.html