read
@abstractmethod
def read(self, n: int = -1) -> AnyStr:
pass
从当前位置读取到文件末尾,带参数则表示读取n个字符
readline
@abstractmethod
def readline(self, limit: int = -1) -> AnyStr:
pass
从当前位置读取到行末,带参数时行为与read
一致
readlines
@abstractmethod
def readlines(self, hint: int = -1) -> List[AnyStr]:
pass
从当前位置读取到文件末尾,带参数时读取hint
个字符,并继续读取直至读入下一个换行符
。
测试
测试文件
111
222
333
444
555
666
777
888
999
输出
a.read(1)
Out[47]: '1'
a.readline()
Out[48]: '11\n'
a.readline(1)
Out[49]: '2'
a.readline()
Out[50]: '22\n'
a.readlines(4)
Out[51]: ['333\n', '444\n']
a.readlines(3)
Out[52]: ['555\n']
a.readlines(7)
Out[53]: ['666\n', '777\n']
a.readlines(8)
Out[54]: ['888\n', '999']
a.seek(0)
Out[55]: 0
a.readlines(8)
Out[56]: ['111\n', '222\n', '333\n']
a.readlines(7)
Out[57]: ['444\n', '555\n']
a.readlines(4)
Out[58]: ['666\n', '777\n']
a.readlines(3)
Out[59]: ['888\n']