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

乌拉乌拉~
Release: 2018-08-16 13:49:38
Original
7049 people have browsed it

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();
Copy after login

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
Copy after login

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()
Copy after login

The output result of the above example is:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template