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://codercto.com/courses/l/10.html

查看所有标签

Modeling the Internet and the Web

Modeling the Internet and the Web

Pierre Baldi、Paolo Frasconi、Padhraic Smyth / Wiley / 2003-7-7 / USD 115.00

Modeling the Internet and the Web covers the most important aspects of modeling the Web using a modern mathematical and probabilistic treatment. It focuses on the information and application layers, a......一起来看看 《Modeling the Internet and the Web》 这本书的介绍吧!

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

在线压缩/解压 JS 代码

SHA 加密
SHA 加密

SHA 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器