Python file methodreadlines()使用readline()读取EOF,并返回包含行的列表。如果存在可选的sizehint参数,则读取总计大约sizehint字节的整行(可能在舍入到内部缓冲区大小之后),而不是读取EOF。
仅当立即遇到EOF时才返回空字符串。
fileObject.readlines( sizehint );
sizehint - 这是要从文件中读取的字节数。
此方法返回包含行的列表。
链接:https://www.learnfk.comhttps://www.learnfk.com/python/file-readlines.html
来源:LearnFk无涯教程网
以下示例显示readlines()方法的用法。
This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line
#!/usr/bin/python # Open a file fo=open("foo.txt", "rw+") print "Name of the file: ", fo.name # Assuming file has following 5 lines # This is 1st line # This is 2nd line # This is 3rd line # This is 4th line # This is 5th line line=fo.readlines() print "Read Line: %s" % (line) line=fo.readlines(2) print "Read Line: %s" % (line) # Close opend file fo.close()
当无涯教程运行上面的程序时,它产生以下输出-
Name of the file: foo.txt Read Line: ['Python is a great language.\n', 'Yeah its great!!\n'] Read Line: []
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)