Computer >> Computer tutorials >  >> Programming >> Python

How do we specify the buffer size when opening a file in Python?


If you look at the function definition of open - open(name[, mode[, buffering]]), you'll see that it takes 3 arguments in Python 2, third one being buffering. The optional buffering argument specifies the file’s desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size (in bytes). A negative buffering means to use the system default, which is usually line buffered for tty devices and fully buffered for other files. If omitted, the system default is used.

For example, If you want to open a file with a buffer size of 128 bytes you can open the file like this −

>>> open('my_file', 'r+', 128)

In Python 3, the function definition of open is: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None). buffering is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode), 1 to select line buffering (only usable in text mode), and an integer > 1 to indicate the size in bytes of a fixed-size chunk buffer. When no buffering argument is given, the default buffering policy works as follows −

  • Binary files are buffered in fixed-size chunks; the size of the buffer is chosen using a heuristic trying to determine the underlying device’s “block size” and falling back on io.DEFAULT_BUFFER_SIZE.

  • “Interactive” text files (files for which isatty() returns True) use line buffering. Other text files use the policy described above for binary files.

The example for Python 3 is the same as Python 2. For example, If you want to open a file with a buffer size of 128 bytes you can open the file like this −

>>> open('my_file', 'r+', 128)