Python参考手册
Python File read() 方法
Python File read() 方法
Python File read() 方法
概述
read() 方法用于从文件读取指定的字节数,如果未给定或为负则读取所有。
语法
read() 方法语法如下:
fileObject.read();
参数
-
size -- 从文件中读取的字节数。
返回值
返回从字符串中读取的字节。
实例
以下实例演示了 read() 方法的使用:
文件 verydoc.txt 的内容如下:
1:www.verydoc.net 2:www.verydoc.net 3:www.verydoc.net 4:www.verydoc.net 5:www.verydoc.net
循环读取文件的内容:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 打开文件 fo = open("verydoc.txt", "rw+") print "文件名为: ", fo.name line = fo.read(10) print "读取的字符串: %s" % (line) # 关闭文件 fo.close()
以上实例输出结果为:
文件名为: verydoc.txt 读取的字符串: 1:www.runo
Python File read() 方法 |
---|

Python 是一种面向对象的解释型计算机程序设计语言,由荷兰人 Guido van Rossum 于1989年发明,第一个公开发行版发行于1991年。 Python 是纯粹的自由软件, 源代码和解释器 CPython 遵循 GPL 协议。Python 语法简洁清晰,特色之一是强制用空白符( white space )作为语句缩进。
主页 | https://www.python.org/ |
源码 | https://github.com/python/cpython |
版本 | 2.7 |
发布版本 | 2.7.13 |