[Python] Can you specify a buffer size when opening a file in text I/O mode

Discussão em 'Python' iniciado por Stack, Janeiro 31, 2025.

  1. Stack

    Stack Membro Participativo

    I'm confused by the documentation regarding the buffering when reading a file opened with the open function.

    Does the buffering parameter do anything when mode is r so that the file is opened in text I/O mode?

    The docs state:


    Note that specifying a buffer size this way applies for binary buffered I/O, but TextIOWrapper (i.e., files opened with mode='r+') would have another buffering.
    ​

    What does another mean? An additional buffering or a different buffering?

    I'd assume it's the latter, which means that the buffering parameter does not specify the size in bytes of a fixed-size chunk buffer. Why would they state that the parameter has a different meaning but not explain it?

    What's the conclusion to this?

    open('myfile.txt', 'r', 1024)

    Does this use a buffer of 1024 bytes? Does the parameter change anything in this mode?

    Continue reading...

Compartilhe esta Página