aingwen 发表于 2014-09-30 10:11

readlines() 参数问题

本帖最后由 aingwen 于 2014-09-30 10:15 编辑

def fy():   #显示3行
    x = raw_input("请输入文件: ")
    with open(x) as f:
      t = f.readlines(3)
      for line in t:
            print line
    return
fy()这段代码我只想显示输入文本的3行,结果却显示出文本的所有行出来,这是为什么?readlines(3) 不是表示读入3行吗?

HH106 发表于 2014-09-30 10:48

readlines(n)
n不是读入的行数,而是一次读入到缓存的字节数.

aingwen 发表于 2014-09-30 11:02

回复 2# HH106
这里我发现把这个参数改成50 或者 500000它都会返回文本全部内容,参数就没起到作用


   

liaozd 发表于 2014-10-03 16:19


file.readlines()¶

    Read until EOF using readline() and return a list containing the lines thus read. If the optional sizehint argument is present, instead of reading up to EOF, whole lines totalling approximately sizehint bytes (possibly after rounding up to an internal buffer size) are read. Objects implementing a file-like interface may choose to ignore sizehint if it cannot be implemented, or cannot be implemented efficiently.


这是一个sizehint不是size,并且是rounding up,你需要知道系统的默认buffer size是多大:
import io
print io.DEFAULT_BUFFER_SIZE

很有可能你单个文件的大小没超超过buffer
页: [1]
查看完整版本: readlines() 参数问题