Python3 File readline() 方法
Python 3 教程
· 2019-02-06 23:29:43
概述
readline() 方法用于从文件读取整行,包括 "\n" 字符。如果指定了一个非负数的参数,则返回指定大小的字节数,包括 "\n" 字符。
语法
readline() 方法语法如下:
fileObject.readline();
参数
-
size -- 从文件中读取的字节数。
返回值
返回从字符串中读取的字节。
实例
以下实例演示了 readline() 方法的使用:
文件 codercto.txt 的内容如下:
1:www.codercto.com 2:www.codercto.com 3:www.codercto.com 4:www.codercto.com 5:www.codercto.com
循环读取文件的内容:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 打开文件
fo = open("codercto.txt", "r+")
print ("文件名为: ", fo.name)
line = fo.readline()
print ("读取第一行 %s" % (line))
line = fo.readline(5)
print ("读取的字符串为: %s" % (line))
# 关闭文件
fo.close()
以上实例输出结果为:
文件名为: codercto.txt 读取第一行 1:www.codercto.com 读取的字符串为: 2:www
点击查看所有 Python 3 教程 文章: https://codercto.com/courses/l/10.html
Rapid Web Applications with TurboGears
Mark Ramm、Kevin Dangoor、Gigi Sayfan / Prentice Hall PTR / 2006-11-07 / USD 44.99
"Dear PHP, It's over between us. You can keep the kitchen sink, but I want my MVC. With TurboGears, I was able to shed the most heinous FileMaker Pro legacy 'solu-tion' imaginable. It has relationshi......一起来看看 《Rapid Web Applications with TurboGears》 这本书的介绍吧!