简单介绍Python中的readline()方法的使用
作者:goldensun 时间:2023-11-02 13:34:30
readline()方法从文件中读取一整行。尾部的换行符保持在字符串中。如果大小参数且非负,那么一个最大字节数,包括结尾的换行和不完整的行可能会返回。
遇到EOF时立即返回一个空字符串。
语法
以下是readline()方法的语法:
fileObject.readline( size );
参数
size -- 这是可以从文件中读取的字节数。
返回值
此方法返回从文件中读取的行。
例子
下面的例子显示了readline()方法的使用。
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "rw+")
print "Name of the file: ", fo.name
# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line
line = fo.readline()
print "Read Line: %s" % (line)
line = fo.readline(5)
print "Read Line: %s" % (line)
# Close opend file
fo.close()
当我们运行上面的程序,它会产生以下结果:
Name of the file: foo.txt
Read Line: This is 1st line
Read Line: This
标签:Python
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
我的快递一个月没动静于是赶紧上线python快递查询系统
2021-04-06 23:49:01
![](https://img.aspxhome.com/file/2023/8/87698_0s.png)
以Python代码实例展示kNN算法的实际运用
2024-05-22 10:28:42
![](https://img.aspxhome.com/file/2023/5/123675_0s.png)
Python 中使用 argparse 解析命令行参数
2023-10-26 14:10:42
解决Python内层for循环如何break出外层的循环的问题
2023-09-07 22:21:39
教你安装python Django(图文)
2023-11-13 14:22:39
![](https://img.aspxhome.com/file/2023/0/63890_0s.png)
一步步详细讲解vue3配置ESLint
2024-06-05 15:31:55
![](https://img.aspxhome.com/file/2023/7/123127_0s.jpg)
JS基于封装函数实现的表格分页完整示例
2024-04-25 13:15:12
![](https://img.aspxhome.com/file/2023/4/135464_0s.png)
MySQL REVOKE实现删除用户权限
2024-01-16 11:29:40
python一行代码合并了162个Word文件
2022-07-24 04:20:57
Python字符串常用方法以及其应用场景详解
2022-02-15 18:39:53
用户体验的另一种认识
2007-10-25 12:36:00
![](https://img.aspxhome.com/file/UploadPic/200710/25/20071025124243541s.jpg)
解析SQL Server 2005 溢用之:合并列值
2009-01-23 14:08:00
Python代码使用 Pyftpdlib实现FTP服务器功能
2022-02-11 16:40:55
![](https://img.aspxhome.com/file/2023/7/99617_0s.webp)
python添加模块搜索路径方法
2023-11-20 09:29:45
小程序实现横向滑动日历效果
2024-04-10 16:19:46
浅析Golang中的内存逃逸
2024-05-05 09:28:21
PHP实现阿里大鱼短信验证的实例代码
2024-05-11 09:52:20
![](https://img.aspxhome.com/file/2023/0/125830_0s.png)
python归并排序算法过程实例讲解
2023-12-02 23:28:06
![](https://img.aspxhome.com/file/2023/0/72170_0s.png)
ubuntu16.04制作vim和python3的开发环境
2022-08-11 00:11:07
ASP:使用ImageMagickObject组件制作缩略图
2008-10-21 12:21:00