Python3 File read() 方法

Python 3 教程 · 2019-02-06 23:13:45

概述

read() 方法用于从文件读取指定的字节数,如果未给定或为负则读取所有。

语法

read() 方法语法如下:

fileObject.read(); 

参数

  • size -- 从文件中读取的字节数。

返回值

返回从字符串中读取的字节。

实例

以下实例演示了 read() 方法的使用:

文件 codercto.txt 的内容如下:

这是第一行
这是第二行
这是第三行
这是第四行
这是第五行

循环读取文件的内容:

#!/usr/bin/python3

# 打开文件
fo = open("codercto.txt", "r+")
print ("文件名为: ", fo.name)

line = fo.read(10)
print ("读取的字符串: %s" % (line))

# 关闭文件
fo.close()

以上实例输出结果为:

文件名为:  codercto.txt
读取的字符串: 这是第一行
这是第二

点击查看所有 Python 3 教程 文章: https://www.codercto.com/courses/l/10.html

查看所有标签

Prometheus: Up & Running

Prometheus: Up & Running

Brian Brazil / O'Reilly Media / 2018-7-9 / USD 49.99

Get up to speed with Prometheus, the metrics-based monitoring system used by tens of thousands of organizations in production. This practical guide provides application developers, sysadmins, and DevO......一起来看看 《Prometheus: Up & Running》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器