Python readlines () method

Source: Internet
Author: User

This method is read until the EOF uses readline () to return a list containing rows. If the optional sizehint parameter exists, instead of reading to EOF, the entire row is approximately sizehint bytes read (possibly rounded to the size of the internal buffer ). syntax: fileObject. readlines (sizehint); parameter: The following is a detailed parameter: sizehint: the number of bytes read from the file. instance :#! /Usr/bin/python # Open a filefo = open ("foo.txt", "r") print "Name of the file:", fo. name # Assuming file has following 5 lines # This is 1st line # This is 2nd line # This is 3rd line # This is 4th line # This is 5th line = fo. readlines () print "Read Line: % s" % (line) line = fo. readlines (2) print "Read Line: % s" % (line) # Close opend filefo. close () will produce the following results: Name of the file: foo.txt Read Line: ['this is 1st line \ n', 'this is 2nd line \ n ', 'This is 3rd line \ n', 'this is 4th line \ n', 'this is 5th line \ n'] Read Line: []

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.