Home  >  Article  >  Backend Development  >  What is readline() python? readline() python definition and usage analysis

What is readline() python? readline() python definition and usage analysis

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

In today’s article, we will learn about the readline method in python. Before entering the article, we must first know what python readline is. Its true writing method should be readline ()python. readline() Where can it be used in python programming and what role can it play. We can learn about these things in today’s article.

Syntax

readline() method syntax is as follows:

fileObject.readline();

Parameters

size --The number of bytes read from the file.

Return Value

Returns the bytes read from the string.

Example

The following example demonstrates the use of the readline() 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:

# -*- coding: UTF-8 -*-
# 打开文件
fo = open("runoob.txt", "rw+")
print "文件名为: ", fo.name
line = fo.readline()
print "读取第一行 %s" % (line)
line = fo.readline(5)
print "读取的字符串为: %s" % (line)
# 关闭文件
fo.close()

The output result of the above example is:

文件名为:  runoob.txt
读取第一行 1:www.runoob.com
读取的字符串为: 2:www

In this article, we understand what the readline() 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 What is readline() python? readline() python definition and usage 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