Latest web development tutorials

Python File readlines () method

Python File (File) method Python File (File) method


Outline

readlines () method is used to read all the rows (until the end symbol EOF) and return a list, if given sizeint> 0, returns the sum of approximately sizeint byte row, the actual read value may be higher than sizhint larger, because the need to fill buffer.

If you hit the end of the symbol EOF empty string is returned.

grammar

readlines () method has the following syntax:

fileObject.readlines( sizehint );

parameter

  • sizehint - number of bytes read from the file.

return value

Returns a list containing all rows.

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.readlines()
print "读取的数据为: %s" % (line)

line = fo.readlines(2)
print "读取的数据为: %s" % (line)


# 关闭文件
fo.close()

The above example output is:

文件名为:  w3big.txt
读取的数据为: ['1:www.w3big.com\n', '2:www.w3big.com\n', '3:www.w3big.com\n', '4:www.w3big.com\n', '5:www.w3big.com\n']
读取的数据为: []

Python File (File) method Python File (File) method