Home > Backend Development > Python Tutorial > What are the differences between .read(), .readline(), and .readlines() in python?

What are the differences between .read(), .readline(), and .readlines() in python?

伊谢尔伦
Release: 2016-11-21 11:37:16
Original
1551 people have browsed it

python中.read(),.readline(),.readlines()的区别:
在python 中,读取写入文件非常简单方便,其中读取文件:
.read():
读取整个文件,通常读取后存入一个字符串变量中,这样要对行进行处理就特别不便,如果文件大于可读大小也不可行
.readline():
每次读取文件中的一行,读取后对该行进行处理
.readlines():
和.readline()有点相像,但是.readlines()是直接读取一整个文件,将文件读取为行列表,然后用for...in...进行处理
eg.

[python] view plain copy print?
f=open("C:\\test.py","r")  
for line in f.readlines():  
    print(line)
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template