File read method of Python file processing (ii)

Source: Internet
Author: User

There are several ways to read the open file of Python in the following ways:

Read ([size]): Reads the file, if the size parameter is passed, reads the size byte, otherwise reads all

ReadLine ([size]): reads a row

ReadLines ([size]): reads the file and returns a list of each line of the file

Cases:

If I have a blogCblog.txt file, the file content is:

Blogcblog

Blog1cblog

Blog2cblog

Read for a second.

1f = open ('BlogCblog.txt')#First, create a file object2FR = F.read ()#read the file contents using the Read () method3 PrintFr#Print What you read4 5 #Printing results:6 #Blogcblog7 #Blog1cblog8 #Blog2cblog

As you can see, the contents of the file read with the read () method are the same as the contents of our original blogCblog.txt, that is, the read () method does not pass in the size parameter when it reads all of it by default.

1 f = open ('blogCblog.txt')  # First create a file object 2 fr = F.read (4)  # reads a file 4 bytes of content with the read () method 3Print fr  #  Print what you read 45# print Result: Blog

Above can see the read () method passed in a parameter ' 4 ', that is, read the contents of the file 4 bytes, through printing can be seen, is actually read to 4 bytes of the Content ' blog '. If you pass in a parameter (size) that is larger than the size of the file, the entire contents of the file are returned.

Now use ReadLine () to read it, or the file:

1 f = open ('blogCblog.txt')  # First create a file object 2 fr = F.readline ()  # reads a row of files with the ReadLine () method 3Print fr  # Print What you read 4 5 # Print Result: Blogcblog

Above you can see the ReadLine () method to read a line of the contents of the file, it is really read to blogcblog this line of content, and then see the size parameter passed in the ReadLine ():

1 f = open ('blogCblog.txt')  # First create a file object 2 fr = F.readline (4)  # 4-byte content content in a row of a file read with the ReadLine () method 3Print fr  #  Print What you read 45# print Result: Blog

The above results are the same as read (4), that is not the difference, it is not, the difference is big, we add a line F.readline (4): (if the parameter passed in (size) is larger than the size of the row, it will return the entire row)

 1  f = open ( " blogcblog.txt    ) #   First create a file object  2  fr = F.readline (4) #   Use the ReadLine () method to read 4-byte content content in a row of a file  3  fr = F.readline (4) #   add another line F.readline (4)  4  print  fr #   What to print read  5  6  #   print result: Cblo  

The third line of the above code adds a row of f.readline (4) and prints the result as cblog. Let's add a few more lines F.readline (4) to see:

1f = open ('BlogCblog.txt')#First, create a file object2FR = F.readline (4)#Use the ReadLine () method to read 4 bytes of content in a row of a file3FR = F.readline (4)#Add another line F.readline (4)4FR1 = F.readline (4)5FR2 = F.readline (4)6FR3 = F.readline (4)7 PrintFR, FR1, FR2, FR3#Print What you read8 9 #Print Result: Cblo gTen #Blog 1Cbl

The above code 4th, 5, 6 lines add a row f.readline (4), and assign value to FR1, FR2, FR3, the result is: Fr Cblo, FR1 for G, FR2 for blog, FR3 for 1CBL, from which can be seen ReadLine () When the parameter is passed in, it reads the number of bytes passed, and when the ReadLine (4) is executed again, it starts reading from the last read, until the line is read, and when the current row is finished reading again using ReadLine (4), the same readline () is the same when no parameters are passed. , that is, a line reading the file, and when ReadLine () is used again, the second line is read.

Then look at the reading of ReadLines ():

1 f = open ('blogCblog.txt')  # First create a file object 2 fr = F.readlines ()  # reads the file with the ReadLines () method 3Print fr  #  Print what you read 45# print Result: [' blogcblog\n ', ' blog1cblog\n ', ' Blog2cblog ']

The above result is really a list of the contents of the file, and then see ReadLines () incoming parameter (size), how to:

 1  f = open ( " blogcblog.txt    ) #   First create a file object  2  fr = F.readlines (4) #   read the file with the ReadLines () method, pass in 4, read 4 bytes  3  print  fr #   print read content  4  5  6  #   print result: [' blogcblog\n ', ' blog1cblog\n ', ' Blog2cblog ']  

Hey? Not read 4 bytes, how to return or list? It's different from the imagination! In fact, the size of Readlins () is different from the size of read (), ReadLine (), and the size of ReadLines () is a python-defined sizehint, via IO. Default_buffer_size can know the size of Python-defined sizehint, in order to verify that the contents of the BlogCblog.txt file is changed to 10,000 lines Blogcblog, the following look at the code:

1 Importio2 Printio. Default_buffer_size3 4f = open ('BlogCblog.txt')#First, create a file object5FR = F.readlines (8192)#read a file using the ReadLines () method, pass in 8192, read 4 bytes6 PrintLen (FR)#The length of the content to be read by the print7 8 9 #Print Result: 8192Ten #820

The above can be seen Python-defined sizehint size of 8192 bytes, while using ReadLines to pass 8192, and assigned to the FR variable, and then print out the length of the FR, Fr 820 (that is, 820 blogcblog), You can also calculate whether the correct: BlogCblog.txt file Blogcblog is 10 bytes (including \ n), 820*10=8200 (bytes), eh, not read 8192 bytes, how to read 8,200 bytes, originally, when reading to 820 lines, Have not finished reading, read only to BL, because it is read line, so will automatically read the No. 820 line complete.

Note: After you have finished working with the file, remember to close the file with the close () method

File read method of Python file processing (ii)

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.