Python:file (Read,readline,readline) How to use

Source: Internet
Author: User

Python read the file, in the use of Readlin, ReadLines will have doubts, the following for you to explain:
Example: The content of A.txt is
AAA 123
BBB 456
Second, first I set a variable:
A= "A.txt"
C=file (a)
Thirdly, we look at the reading results using read, ReadLine, ReadLines, respectively:
(1), read:
In:c.read ()
Out: '
So:read each time a file is read, it is usually put into a string variable, that is, the content of the. Read () generated file is a string type.

(2), ReadLine:
In:c.readline ()
Out: ' AAA 123\n '
So:readline each read a single line of the file, usually a line of text that is read into a string variable, and the type of STR is returned.

(3), ReadLines:
In:c.readlines ()
Out: [' AAA 123\n ', ' BBB 456\n ']
So:readlines reads the entire contents of the file per row, places the read into a list, and returns the list type.

This article from the "11421725" blog, reproduced please contact the author!

Python:file (Read,readline,readline) How to use

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.