File函数 中的 file.read([size])

首页 / Python2入门教程 / File函数 中的 file.read([size])

Python file方法read()从文件中最多读取size字节。如果读取在获取大小字节之前命中EOF,则它只读取可用字节。

file.read([size]) - 语法

下面是Read()方法-的语法

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

file.read([size]) - 返回值

此方法返回以字符串形式读取的字节。

链接:https://www.learnfk.comhttps://www.learnfk.com/python/file-read.html

来源:LearnFk无涯教程网

file.read([size]) - 示例

以下示例显示read()方法的用法。

无涯教程网

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.read(10)
print "Read Line: %s" % (line)

# Close opend file
fo.close()

当无涯教程运行上面的程序时,它产生以下输出-

Name of the file:  foo.txt
Read Line: Python is 

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

技术教程推荐

大规模数据处理实战 -〔蔡元楠〕

ZooKeeper实战与源码剖析 -〔么敬国〕

移动端自动化测试实战 -〔思寒〕

现代C++编程实战 -〔吴咏炜〕

Spark核心原理与实战 -〔王磊〕

手把手教你玩音乐 -〔邓柯〕

Kubernetes入门实战课 -〔罗剑锋〕

超级访谈:对话道哥 -〔吴翰清(道哥)〕

Rust 语言从入门到实战 -〔唐刚〕

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