File函数 中的 file.readlines([sizehi

首页 / Python2入门教程 / File函数 中的 file.readlines([sizehi

Python file methodreadlines()使用readline()读取EOF,并返回包含行的列表。如果存在可选的sizehint参数,则读取总计大约sizehint字节的整行(可能在舍入到内部缓冲区大小之后),而不是读取EOF。

仅当立即遇到EOF时才返回空字符串。

无涯教程网

file.readlines - 语法

fileObject.readlines( sizehint );
  • sizehint  -  这是要从文件中读取的字节数。

file.readlines - 返回值

此方法返回包含行的列表。

file.readlines - 示例

以下示例显示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: []

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Java并发编程实战 -〔王宝令〕

DevOps实战笔记 -〔石雪峰〕

性能测试实战30讲 -〔高楼〕

Vim 实用技巧必知必会 -〔吴咏炜〕

MySQL 必知必会 -〔朱晓峰〕

零基础入门Spark -〔吴磊〕

大厂设计进阶实战课 -〔小乔〕

Go进阶 · 分布式爬虫实战 -〔郑建勋〕

云原生基础架构实战课 -〔潘野〕

好记忆不如烂笔头。留下您的足迹吧 :)