Python File readlines () usage, pythonreadlines

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.