Home  >  Article  >  Backend Development  >  Python read() method definition and usage (example analysis)

Python read() method definition and usage (example analysis)

乌拉乌拉~
乌拉乌拉~Original
2018-08-16 13:49:386991browse

In today’s article, let’s learn about the pythonread method. It doesn’t matter if you don’t know. Because what we are talking about today is the read() method in python, and knowing what read means, so let’s learn about it in today’s article.

Overview

The read() method is used to read the specified number of bytes from the file, or all if not given or negative.

Syntax

read() method syntax is as follows:

fileObject.read();

Parameters

size --The number of bytes read from the file

Return value

Returns the number of bytes read from the string .

Example

The following example demonstrates the use of the read() method:

The content of the file runoob.txt is as follows:

1:www.runoob.com
2:www.runoob.com
3:www.runoob.com
4:www.runoob.com
5:www.runoob.com

Loop to read the contents of the file:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 打开文件
fo = open("runoob.txt", "rw+")
print "文件名为: ", fo.name
line = fo.read(10)
print "读取的字符串: %s" % (line)
# 关闭文件
fo.close()

The output result of the above example is:

文件名为:  runoob.txt
读取的字符串: 1:www.runo

In this article, we understand what the read() method is. If you don’t understand, in fact You can try it yourself. After all, hands-on practice is the best way to verify what you have learned. Finally, I hope this article can bring some help to you who are learning python.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of Python read() method definition and usage (example analysis). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn