Python File readlines () usage, pythonreadlines
Overview
readlines()
The method is used to read all rows (until the end of EOF) and return a list. The list can be processed by the structure of Python.
If the EOF operator is met, an empty string is returned.
Syntax
readlines()
The method syntax is as follows:
FileObject. readlines ();
Parameters
None.
Return Value
The returned list contains all rows.
Instance
The following example demonstrates the use of the readline () method:
The contents of the jb51.txt file are as follows:
1: www.jb51.net
2: www.jb51.net
3: www.jb51.net
4: www.jb51.net
5: www.jb51.net
Read the object content cyclically:
Python2
#! /Usr/bin/python #-*-coding: UTF-8-*-# open the file fo = open ("jb51.txt", "r") print "file name:", fo. name for line in fo. readlines (): # Read line = line in sequence. strip () # Remove blank print at the beginning and end of each line "read data: % s" % (line) # close the file fo. close ()
Python3
#-*-Coding: UTF-8-*-# open the file fo = open ("jb51.txt", "r") print ("file name:", fo. name) for line in fo. readlines (): # Read line = line in sequence. strip () # Remove the blank print at the beginning and end of each line ("read data: % s" % (line) # close the file fo. close ()
Shows the effect.
This article introduces you here. For more information, see