Python File readline () method
Outline
readline () method is used to read the entire line from the file, including the "\ n" character.If a non-negative parameter is specified, then returns the number of bytes specified size, including "\ n" character.
grammar
readline () method has the following syntax:
fileObject.readline();
parameter
size - the number of bytes read from the file.
return value
It returns the byte read from a string.
Examples
The following example demonstrates the readline () method of use:
W3big.txt content file as follows:
1:www.w3big.com 2:www.w3big.com 3:www.w3big.com 4:www.w3big.com 5:www.w3big.com
Loop reads the contents of the file:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 打开文件 fo = open("w3big.txt", "rw+") print "文件名为: ", fo.name line = fo.readline() print "读取第一行 %s" % (line) line = fo.readline(5) print "读取的字符串为: %s" % (line) # 关闭文件 fo.close()
The above example output is:
文件名为: w3big.txt 读取第一行 1:www.w3big.com 读取的字符串为: 2:www