In python, The readline method is used to determine the end of File Reading,
This example describes how to determine the end of reading a file through readline in python. Share it with you for your reference. The specific analysis is as follows:
As you know, you can use the readline function to read files by row in python. The following describes how to read files by row traversal. Through this method, we will discuss the following issues:
Copy codeCode: filename = raw_input ('enter your file name') # Enter the path and file name of the file to be retrieved
File = open (filename, 'R ')
Done = 0
While not done:
ALine = file. readline ()
If (aLine! = ''):
Print aLine,
Else:
Done = 1
File. close () # close the file
The above is a method we often see to traverse a file by line. You may have noticed the if (aLine! = ''): Part. When readline reads null, it means that the read ends. At this time, the question is here. Many people may wonder if a blank line will be considered as the end of the file? This introduces the title issue.
In fact, the blank line of the file does not return a blank line. Because there is one or more delimiters at the end of each line, "blank lines" have at least one line break or other symbols used by the system. Therefore, even if the file really contains a blank line, the read row is not empty, which means that the program will not actually stop until the actual traversal is read to the end of the file.
Readline () and. readlines () are very similar. They are all used in a structure similar to the following:
Python. readlines ()
Example:
Copy codeThe Code is 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 a time, like. read .. Readlines () automatically analyzes the file content into a list of rows, which can be processed by the structure of Python for... in. On the other hand,. readline () reads only one row at a time, which is usually much slower than. readlines. Use. readline () only when there is not enough memory to read the entire file at a time ().
Number of rows returned by readlines
The official document is written as follows:
If the optional sizehint argument is present, instead of reading up to EOF, whole lines totalling approximately sizehint bytes (possibly after rounding up to an internal buffer size) are read.
It is indeed the specified size and will be affected by the size of the internal buffer and rounded up to the size of the internal buffer. The internal buffer is about 8 k. No wonder that each test file is a multiple of 8 k (8192 ).
Copy codeThe Code is 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 Python programming.