在Python中操作文件之read()方法的使用教程


Posted in Python onMay 24, 2015

 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 相关文章推荐
Python FTP操作类代码分享
May 13 Python
Python常用正则表达式符号浅析
Aug 13 Python
Python编程中的反模式实例分析
Dec 08 Python
在Python中处理XML的教程
Apr 29 Python
python简单的函数定义和用法实例
May 07 Python
django站点管理详解
Dec 12 Python
tensorflow实现简单的卷积网络
May 24 Python
浅析使用Python搭建http服务器
Oct 27 Python
Django Channel实时推送与聊天的示例代码
Apr 30 Python
python环境搭建和pycharm的安装配置及汉化详细教程(零基础小白版)
Aug 19 Python
python 如何对logging日志封装
Dec 02 Python
python爬虫scrapy基本使用超详细教程
Feb 20 Python
在Python中使用next()方法操作文件的教程
May 24 #Python
在Python程序中操作文件之isatty()方法的使用教程
May 24 #Python
讲解Python中fileno()方法的使用
May 24 #Python
在Python程序中操作文件之flush()方法的使用教程
May 24 #Python
Python编程中用close()方法关闭文件的教程
May 24 #Python
详细讲解Python中的文件I/O操作
May 24 #Python
详解在Python中处理异常的教程
May 24 #Python
You might like
php数据库备份还原类分享
2014/03/20 PHP
PHP判断一个gif图片是否为动态图片的方法
2014/11/19 PHP
php准确获取文件MIME类型的方法
2015/06/17 PHP
JavaScript 密码强度判断代码
2009/09/05 Javascript
百度留言本js 大家可以参考下
2009/10/13 Javascript
JavaScript中的getMilliseconds()方法使用详解
2015/06/10 Javascript
jQuery实现可编辑的表格实例讲解(2)
2015/09/17 Javascript
基于jQuery实现点击最后一行实现行自增效果的表格
2016/01/12 Javascript
基于javascript实现图片滑动效果
2016/05/07 Javascript
Angularjs 制作购物车功能实例代码
2016/09/14 Javascript
详解使用vue脚手架工具搭建vue-webpack项目
2017/05/10 Javascript
提高Node.js性能的应用技巧分享
2017/08/10 Javascript
vue路由拦截及页面跳转的设置方法
2018/05/24 Javascript
React父子组件间的传值的方法
2018/11/13 Javascript
python遍历类中所有成员的方法
2015/03/18 Python
python使用psutil模块获取系统状态
2016/08/27 Python
利用Python脚本实现ping百度和google的方法
2017/01/24 Python
5个很好的Python面试题问题答案及分析
2018/01/19 Python
python 执行shell命令并将结果保存的实例
2018/05/11 Python
python3 读取Excel表格中的数据
2018/10/16 Python
Python3.5 Pandas模块之Series用法实例分析
2019/04/23 Python
pandas将多个dataframe以多个sheet的形式保存到一个excel文件中
2019/10/10 Python
jupyternotebook 撤销删除的操作方式
2020/04/17 Python
python openCV实现摄像头获取人脸图片
2020/08/20 Python
Python QT组件库qtwidgets的使用
2020/11/02 Python
Python日志打印里logging.getLogger源码分析详解
2021/01/17 Python
中国排名第一的外贸销售网站:LightInTheBox.com(兰亭集势)
2016/10/28 全球购物
数以千计的折扣工业产品:ESE Direct
2018/05/20 全球购物
生物化工工艺专业应届生求职信
2013/10/08 职场文书
医药专业推荐信
2013/11/15 职场文书
淘宝中秋节活动方案
2014/01/31 职场文书
本科毕业论文指导教师评语
2014/12/30 职场文书
2015年教研工作总结
2015/05/23 职场文书
使用CSS3实现按钮悬停闪烁动态特效代码
2021/08/30 HTML / CSS
NASA 机智号火星直升机拍到了毅力号设备碎片
2022/04/29 数码科技
pytest实现多进程与多线程运行超好用的插件
2022/07/15 Python