python的read() 、readline()、readlines()、xreadlines()
2013-10-10 14:41
405 查看
先来一个小例子:
import sys
dir= os.path.dirname(os.path.abspath(__file__))
file_path='%s/test.txt' % dir
f= open(file_path,'r')
#print f.read()
print "----------------"
for line in f.readlines():
line.strip()
print line
#f.write('testxxx3xx33333333333')
#print filet.read()
f.close()
输出结果为:
----------------
fucttttx3tttt22221111133
fucttttx3tttt22221111133
fucttttx3tttt22221111133
其实,python文件读入函数有read(), readline(), readlines() & xreadlines() func in Python
介绍如下:
* read(size) >> size is an optional numeric argument and this func returns a quantity of data equal to size. If size if omitted, then it reads the entire file and returns it
读入指定大小的内容,以byte为单位,size为读入的字符数,返回str类型
注意省略时,read()会读取整个文件,将读取到底的文件内容放到一个字符串变量,返回str类型。
* readline() >> reads a single line from file with newline at the end。
readline()读取一行内容,放到一个字符串变量,返回str类型。
* readlines() >> returns a list containing all the lines in the file
readlines() 读取文件所有内容,按行为单位放到一个列表中,返回list类型。
* xreadlines() >> Returns a generator to loop over every single line in the file
返回一个生成器,来循环操作文件的每一行。循环使用时和readlines基本一样,但是直接打印就不同
print f.xreadlines()
print f.readlines()
输出如下:
<open file '/home/deve_test_user/liu/test.txt', mode 'r' at 0x2b1e19035cd8>
['f3tttt22221111133\n', 'fucttttx3tttt22221111133\n', 'fucttttx3tttt22221111133\n', 'fucttttx3tttt22221111133\n', 'fucttttx3tttt22221111133\n', 'fucttttx3tttt22221111133\n', 'fucttttx3tttt22221111133\n', 'fttx3tttt2222111113endend\n']
所以二者类型不同。但使用时相同。
import sys
dir= os.path.dirname(os.path.abspath(__file__))
file_path='%s/test.txt' % dir
f= open(file_path,'r')
#print f.read()
print "----------------"
for line in f.readlines():
line.strip()
print line
#f.write('testxxx3xx33333333333')
#print filet.read()
f.close()
输出结果为:
----------------
fucttttx3tttt22221111133
fucttttx3tttt22221111133
fucttttx3tttt22221111133
其实,python文件读入函数有read(), readline(), readlines() & xreadlines() func in Python
介绍如下:
* read(size) >> size is an optional numeric argument and this func returns a quantity of data equal to size. If size if omitted, then it reads the entire file and returns it
读入指定大小的内容,以byte为单位,size为读入的字符数,返回str类型
注意省略时,read()会读取整个文件,将读取到底的文件内容放到一个字符串变量,返回str类型。
* readline() >> reads a single line from file with newline at the end。
readline()读取一行内容,放到一个字符串变量,返回str类型。
* readlines() >> returns a list containing all the lines in the file
readlines() 读取文件所有内容,按行为单位放到一个列表中,返回list类型。
* xreadlines() >> Returns a generator to loop over every single line in the file
返回一个生成器,来循环操作文件的每一行。循环使用时和readlines基本一样,但是直接打印就不同
print f.xreadlines()
print f.readlines()
输出如下:
<open file '/home/deve_test_user/liu/test.txt', mode 'r' at 0x2b1e19035cd8>
['f3tttt22221111133\n', 'fucttttx3tttt22221111133\n', 'fucttttx3tttt22221111133\n', 'fucttttx3tttt22221111133\n', 'fucttttx3tttt22221111133\n', 'fucttttx3tttt22221111133\n', 'fucttttx3tttt22221111133\n', 'fttx3tttt2222111113endend\n']
所以二者类型不同。但使用时相同。
相关文章推荐
- python的read() 、readline()、readlines()、xreadlines()
- python中read() readline()以及readlines()对比
- python中的三个读read(),readline()和readlines()
- python中read()、readline()和readlines()的区别
- python中read() readline()以及readlines()用法
- Python中read()、readline()和readlines()三者间的区别和用法
- Python小知识积累之read,readline,readlines
- python read() readline() readlines() write() writelines()方法总结
- python读文件read()、readline()、readlines()三个方法的区别
- python 读取文件f.read()、f.readline()和f.readlines()
- python中read() readline()以及readlines()区别
- 【Python】文件操作,read(), readline(), readlines()
- python:大文件迭代器,GB级别,read(),readline(),readlines()以及语法糖with open()
- [Python] extend与append,readlines,read,readline
- Python中read,readline,readlines三种方式的区别
- python中read() readline()以及readlines()区别 .
- python中read() readline()以及readlines()用法
- python 中的read,readline,readlines
- Python中read()、readline()和readlines()三者间的区别和用法
- python读操作:read(),readline(),readlines() 的区别