先来一个小例子:

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()的更多相关文章

  1. python read readline readlines区别

    file 对象使用 open 函数来创建,下表列出了 file 对象常用函数read.readline.readlines区别: 1.从文件读取指定的字节数,size如果未给定或为负则读取所有. fi ...

  2. python中read() readline()以及readlines()用法

    [转自:http://www.ibm.com/developerworks/cn/linux/sdk/python/python-5/index.html#N1004E] 我们谈到“文本处理”时,我们 ...

  3. [python]文件操作read&readline&readlines

    (1)read是将整个文件读入内存,将整个文件的内容当作一个字符串 (2)readline是一行一行的读如内存,每一次读的一行为一个字符串 (3)readlines是一次将整个文件读入内存,但是将整个 ...

  4. Python中文件读写read,readline,readlines函数的区别?

    read 每次会读取整个文件 readline 每次读取一行信息 readlines 读取整个文件返回一个列表,列表每个元素代表一行

  5. python中判断readline读到文件末尾

    fp = open('somefile.txt') while True: line = fp.readline() if not line: #等价于if line == "": ...

  6. read,readline,readlines的特点与区别

    1.read 读取全部文件 with open("test.text", "r",encoding='utf8') as f: print(f.read()) ...

  7. readline,readlines,read函数

    readline是读取每一行,包括'\n'.读出来是一个含'\n'的字符串. realines是读取整个文件,返回所有行的一个list(写代码的时候你需要一个文件的某几行,就可以用这个函数去切分) r ...

  8. Python中深浅拷贝 垃圾回收与 super继承(六)

    1 python拷贝 深拷贝,浅拷贝 与引用三者的区别 import copy a = [1, 2, 3, 4, ['a', 'b']] #原始对象 b = a #赋值,传对象的引用 c = copy ...

  9. day7_python学习笔记_chapter9_文件

    1. open(), file(), 作用完全相同 2. 语法: file_object = open(file_name, access_mode='r', buffering='-1') acce ...

随机推荐

  1. Oracle中打印99乘法表的13种方法

    --实现1: select r1 || '*' || r1 || '=' || r1 * r1 A, decode(r2, '', '', r2 || '*' || r1 || '=' || r2 * ...

  2. python中input()和raw_input()的区别

    两者均是python的内置函数,通过读取控制台的输入与用户实现交互.raw_input:将所有输入作为字符串看待,不管用户输入什么类型的都会转变成字符串.                   raw的 ...

  3. Node.js 进程

    process 是全局对象,能够在任意位置访问,是 EventEmitter 的实例. 退出状态码 当没有新的异步的操作等待处理时,Node 正常情况下退出时会返回状态码 0 .下面的状态码表示其他状 ...

  4. Go 语言结构体

    Go 语言中数组可以存储同一类型的数据,但在结构体中我们可以为不同项定义不同的数据类型. 结构体是由一系列具有相同类型或不同类型的数据构成的数据集合. 结构体表示一项记录,比如保存图书馆的书籍记录,每 ...

  5. git 覆盖本地变化

    git fetch && git reset --hard origin/master

  6. (译)快速指南:用UIViewPropertyAnimator做动画

    翻译自:QUICK GUIDE: ANIMATIONS WITH UIVIEWPROPERTYANIMATOR 译者:Haley_Wong iOS 10 带来了一大票有意思的新特性,像 UIViewP ...

  7. logstash输出到elasticsearch多索引

    目标:将json格式的两类日志输出到elasticsearch两类索引 1. 安装logstash. 2. 编写logstash处理配置文件,创建一个test.conf文件,内容如下: input { ...

  8. Java使用agent实现main方法之前

    创建Agent项目 PreMainExecutor 类,在main方法之前执行此方法 public class PreMainExecutor { public static void premain ...

  9. JAVA面向对象-----instanceof 关键字

    instanceof 关键字 1:快速演示instanceof Person p=new Person(); System.out.println( p instanceof Person); 2:i ...

  10. 剑指Offer——京东校招笔试题+知识点总结

    剑指Offer--京东校招笔试题+知识点总结 笔试感言 经过一系列的笔试,发觉自己的基础知识还是比较薄弱的,尤其是数据结构和网络,还有操作系统.工作量还是很大的.做到精确制导的好方法就是在网上刷题,包 ...