本文实例讲述了python中readline判断文件读取结束的方法。分享给大家供大家参考。具体分析如下:
大家知道,python中按行读取文件可以使用readline函数,下面现介绍一个按行遍历读取文件的方法,通过这个方法,展开我们要讨论的问题:
file = open(filename,'r')
done = 0
while not done:
aLine = file.readline()
if(aLine != ''):
print aLine,
else:
done = 1
file.close() #关闭文件
上面是我们经常看到的按行遍历一个文件方法,你可能已经注意到我在代码中写的if(aLine != '' ):部分。当readline读取到为空的时候,意味着读到了文件的结束。这个时候,问题就在这里,很多人会想,是不是遇到一个空行,也会被认为是文件的结束呢?这就引入了标题的问题。
事实上,文件的空白行并不会返回一个空行。因为在每一行的末尾还有一个或者多个分隔符,因此“空白行”至少会有一个换行符或者系统使用的其他符号。所以,即使文件中真的包含一个“空白行”,读入的行也不是空的,这就意味着在真实遍历读取到文件结束之前,程序实际上是不会停止的
readline() 和 .readlines() 非常相似。它们都在类似于以下的结构中使用:
Python .readlines()
示例如下:
for line in fh.readlines():
print line
.readline() 和 .readlines() 之间的差异是后者一次读取整个文件,象 .read() 一样。.readlines() 自动将文件内容分析成一个行的列表,该列表可以由 Python 的 for ... in ... 结构进行处理。另一方面,.readline() 每次只读取一行,通常比 .readlines() 慢得多。仅当没有足够内存可以一次读取整个文件时,才应该使用 .readline()。
readlines返回行数问题
官方文档这样写的:
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.
确实是指定大小啊并且会受内部缓冲区大小影响向上取整到内部缓冲区大小。内部缓冲区大约是8k也难怪我每次测试文件大小都是8k(8192)倍数
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))
希望本文所述对大家的Python程序设计有所帮助。

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft