A method of readline in Python to judge the end of a file read _python

Source: Internet
Author: User
Tags readline

The examples in this article describe the method of readline in Python to judge the end of a file read. Share to everyone for your reference. The specific analysis is as follows:

As you know, the ReadLine function is available for reading files by row in Python, and here's a way to read a file by row, and by doing this, start the question we're going to discuss:

Copy Code code as follows:
filename = raw_input (' Enter your file name ') #输入要遍历读取的文件路径及文件名
FILE = open (filename, ' R ')
Done = 0
While does not do:
ALine = File.readline ()
if (ALine!= '):
Print ALine,
Else
Done = 1
File.close () #关闭文件

The above is what we often see by line traversing a file method, and you may have noticed the if (aLine!= ') I wrote in the code: part. When ReadLine reads to be empty, it means reading the end of the file. This time, the problem is here, many people will think, is not encountered a blank line, will also be considered as the end of the file? This introduces the question of the title.

In fact, a blank row of files does not return a blank line. Because there is one or more delimiters at the end of each line, the blank line will have at least one line break or other symbol used by the system. So, even if the file really contains a "blank line", the read line is not empty, which means that the program actually does not stop until the actual traversal is read to the end of the file.

ReadLine () and. ReadLines () are very similar. They are all used in structures similar to the following:
Python. ReadLines ()

Examples are as follows:

Copy Code code as follows:
FH = open (' C:\autoexec.bat ')
For line in Fh.readlines ():
Print Line


The difference between ReadLine () and. ReadLines () is that the latter reads the entire file at once, like. Read (). ReadLines () automatically analyzes the contents of the file into a list of rows that can be used by Python for ... Structure for processing. On the other hand,. ReadLine () reads only one row at a time, usually much slower than. ReadLines (). You should use the. ReadLine () only if there is not enough memory to read the entire file at once.

ReadLines return row count problem

The Official document reads:
If The optional sizehint argument is present, instead of reading up to EOF, whole lines totalling approximately-Sizehi NT bytes (possibly after rounding up to a internal buffer size) are read.

It is indeed a specified size and will be affected by the internal buffer size up to the internal buffer size. The internal buffer is about 8k. Every time I test the file size is a multiple of 8k (8192)

Copy Code code as follows:
#!/usr/bin/env python
F=open (' A.txt '). ReadLines (1)
Open (' B.txt ', ' W '). Writelines (f)
Open (' C.txt ', ' W '). Writelines (Open (' a.txt '). ReadLines (200))
Open (' D.txt ', ' W '). Writelines (Open (' a.txt '). ReadLines (9200))
Open (' E.txt ', ' W '). Writelines (Open (' a.txt '). ReadLines (26000))
Open (' F.txt ', ' W '). Writelines (Open (' a.txt '). ReadLines (40000))

I hope this article will help you with your Python programming.

Related Article

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.