在Python中操作文件之read()方法的使用教程
作者:goldensun 时间:2021-10-28 19:23:46
read()方法读取文件size个字节大小。如果读取命中获得EOF大小字节之前,那么它只能读取可用的字节。
语法
以下是read()方法的语法:
fileObject.read( size );
参数
size -- 这是可以从文件中读取的字节数。
返回值
此方法返回读取字符串中的字节数。
例子
下面的例子显示了read()方法的使用。
#!/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.read(10)
print "Read Line: %s" % (line)
# Close opend file
fo.close()
当我们运行上面的程序,它会产生以下结果:
Name of the file: foo.txt
Read Line: This is 1s
标签:Python
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python写的贪吃蛇游戏例子
2023-06-26 15:50:55
django 邮件发送模块smtp使用详解
2021-09-24 23:04:15
详细解读Python的web.py框架下的application.py模块
2021-06-24 22:28:47
上传组件 ASPUpload 使用说明
2008-09-11 14:38:00
用javascript给表格加滚动条
2008-10-15 10:03:00
用Dreamweaver MX巧妙格式化表格
2008-03-18 16:39:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113211174s.jpg)
Web Design广告字体设计技巧
2010-06-24 21:52:00
通过Python实现控制手机详解
2021-04-21 12:10:37
![](https://img.aspxhome.com/file/2023/2/69552_0s.png)
网页设计者应当注意九大要点
2007-08-10 13:30:00
利用Python实现sqlite3增删改查的封装
2021-06-19 14:57:05
全面了解CSS内置颜色(color)值
2008-11-19 12:26:00
秒杀场景的缓存、队列、锁使用Redis优化设计方案
2023-05-29 19:07:18
![](https://img.aspxhome.com/file/2023/9/55339_0s.png)
tornado捕获和处理404错误的方法
2023-11-27 11:03:36
Laravel框架文件上传功能实现方法示例
2023-11-15 19:16:31
![](https://img.aspxhome.com/file/2023/7/89717_0s.png)
实例讲解Python中SocketServer模块处理网络请求的用法
2021-04-16 13:25:27
如何用ASP输出HTML文件?
2010-06-11 20:01:00
PHP基于非递归算法实现先序、中序及后序遍历二叉树操作示例
2023-08-16 04:46:47
![](https://img.aspxhome.com/file/2023/7/55387_0s.jpg)
python通过索引遍历列表的方法
2021-07-06 06:06:53
SQL Server实现分布式数据库系统的终极目标
2010-08-05 14:57:00
网页设计之关于素材和言志
2008-03-23 13:46:00
![](https://img.aspxhome.com/file/UploadPic/20083/23/200832313514542s.gif)