Is there any way to find the buffer size of a file object

后端 未结 1 2080
北海茫月
北海茫月 2021-01-06 09:22

I\'m trying to \"map\" a very large ascii file. Basically I read lines until I find a certain tag and then I want to know the position of that tag so that I can seek to it

1条回答
  •  隐瞒了意图╮
    2021-01-06 09:34

    To me, it looks like the buffer size is hard-coded in Cpython to be 8192. As far as I can tell, there is no way to get this number from the python interface other than to read a single line when you open the file, do a f.tell() to figure out how much data python actually read and then seek back to the start of the file before continuing.

    with open(datafile) as fin:
        next(fin)
        bufsize = fin.tell()
        fin.seek(0)
    
        ifin = dropwhile(lambda x:not x.startswith('Foo'), fin)
        header = next(ifin)
        position = fin.tell()
    

    Of course, this fails in the event that the first line is longer than 8192 bytes long, but that's not of any real consequence for my application.

    0 讨论(0)
提交回复
热议问题