🌐
Python
docs.python.org › 3 › library › io.html
io — Core tools for working with streams
The io module provides Python’s main facilities for dealing with various types of I/O. There are three main types of I/O: text I/O, binary I/O and raw I/O. These are generic categories, and various backing stores can be used for each of them. A concrete object belonging to any of these categories is called a file object.
🌐
Real Python
realpython.com › ref › stdlib › io
io | Python Standard Library – Real Python
The Python io module provides tools for dealing with various types of input/output (I/O), including reading and writing files, handling binary data, and working with streams.
🌐
AskPython
askpython.com › home › python io module: the complete practical reference
Python IO Module: The Complete Practical Reference - AskPython
February 16, 2023 - The io.open() function is a much preferred way to perform I/O operations as it is made as a high-level Pythonic interface.
🌐
PyPI
pypi.org › project › Python-IO
Python-IO · PyPI
Python-IO is a Python module for working with user I/O. It has features for accepting user input and showing output.
      » pip install Python-IO
    
Published   Jan 04, 2023
Version   0.3
🌐
W3Schools
w3schools.com › Python › ref_module_io.asp
Python io Module
Python Examples Python Compiler ... Python Bootcamp Python Training ... The io module provides Python's main facilities for dealing with streams (text, binary, buffered)....
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-io-bytesio-stringio
Python io.BytesIO and io.StringIO: Memory File Guide | DigitalOcean
August 3, 2022 - Use Python io.BytesIO and io.StringIO for in-memory operations. Complete guide with examples for binary, string data handling and performance tips.
🌐
Tutorialspoint
tutorialspoint.com › python › python_files_io.htm
Python - Files I/O
This chapter covers all the basic I/O functions available in Python. For more functions, please refer to standard Python documentation. The simplest way to produce output is using the print statement where you can pass zero or more expressions
🌐
GitHub
github.com › python › cpython › blob › main › Lib › io.py
cpython/Lib/io.py at main · python/cpython
"""The io module provides the Python interfaces to stream handling.
Author   python
🌐
Solomonmarvel
pythonforstarters.solomonmarvel.com › directory-and-io › python-io-module
Python IO Module | Python For Starters
January 23, 2023 - In Python, the io module provides a uniform interface for reading and writing streams of data.
Find elsewhere
🌐
Python
docs.python.org › 3.10 › library › io.html
io — Core tools for working with streams — Python 3.10.20 documentation
The io module provides Python’s main facilities for dealing with various types of I/O. There are three main types of I/O: text I/O, binary I/O and raw I/O. These are generic categories, and various backing stores can be used for each of them. A concrete object belonging to any of these categories ...
🌐
Readthedocs
docspy3zh.readthedocs.io › en › latest › library › io.html
15.2. io — Core tools for working with streams — Python 3 文档(简体中文) 3.2.2 documentation
The io module provides Python’s main facilities for dealing for various types of I/O. There are three main types of I/O: text I/O, binary I/O, raw I/O. These are generic categories, and various backing stores can be used for each of them.
🌐
Cornell Virtual Workshop
cvw.cac.cornell.edu › python-intro › input-output › file-io
Cornell Virtual Workshop > Introduction to Python Programming > Input/Output > File I/O
Because an open file must be closed — even though closing typically takes place after multiple intermediate statements — a better approach to managing file access involves with the Python keyword with. A with statement is used to wrap the execution of a block with methods defined by what is known as a context manager.
Top answer
1 of 2
208

For simplicity's sake, let's consider writing instead of reading for now.

So when you use open() like say:

with open("test.dat", "wb") as f:
    f.write(b"Hello World")
    f.write(b"Hello World")
    f.write(b"Hello World")

After executing that a file called test.dat will be created, containing 3x Hello World. The data wont be kept in memory after it's written to the file (unless being kept by a name).

Now when you consider io.BytesIO() instead:

with io.BytesIO() as f:
    f.write(b"Hello World")
    f.write(b"Hello World")
    f.write(b"Hello World")

Which instead of writing the contents to a file, it's written to an in memory buffer. In other words a chunk of RAM. Essentially writing the following would be the equivalent:

buffer = b""
buffer += b"Hello World"
buffer += b"Hello World"
buffer += b"Hello World"

In relation to the example with the with statement, then at the end there would also be a del buffer.

The key difference here is optimization and performance. io.BytesIO is able to do some optimizations that makes it faster than simply concatenating all the b"Hello World" one by one.

Just to prove it here's a small benchmark:

  • Concat: 1.3529 seconds
  • BytesIO: 0.0090 seconds

import io
import time

begin = time.time()
buffer = b""
for i in range(0, 50000):
    buffer += b"Hello World"
end = time.time()
seconds = end - begin
print("Concat:", seconds)

begin = time.time()
buffer = io.BytesIO()
for i in range(0, 50000):
    buffer.write(b"Hello World")
end = time.time()
seconds = end - begin
print("BytesIO:", seconds)

Besides the performance gain, using BytesIO instead of concatenating has the advantage that BytesIO can be used in place of a file object. So say you have a function that expects a file object to write to. Then you can give it that in-memory buffer instead of a file.

The difference is that open("myfile.jpg", "rb") simply loads and returns the contents of myfile.jpg; whereas, BytesIO again is just a buffer containing some data.

Since BytesIO is just a buffer - if you wanted to write the contents to a file later - you'd have to do:

buffer = io.BytesIO()
# ...
with open("test.dat", "wb") as f:
    f.write(buffer.getvalue())

Also, you didn't mention a version; I'm using Python 3. Related to the examples: I'm using the with statement instead of calling f.close()

2 of 2
42

Using open opens a file on your hard drive. Depending on what mode you use, you can read or write (or both) from the disk.

A BytesIO object isn't associated with any real file on the disk. It's just a chunk of memory that behaves like a file does. It has the same API as a file object returned from open (with mode r+b, allowing reading and writing of binary data).

BytesIO (and it's close sibling StringIO which is always in text mode) can be useful when you need to pass data to or from an API that expect to be given a file object, but where you'd prefer to pass the data directly. You can load your input data you have into the BytesIO before giving it to the library. After it returns, you can get any data the library wrote to the file from the BytesIO using the getvalue() method. (Usually you'd only need to do one of those, of course.)

🌐
Medium
medium.com › python4you › python-io-streams-in-examples-97d2c4367207
Python IO streams in examples. Python IO streams: BytesIO and StringIO… | by Artem Rys | python4you | Medium
December 27, 2018 - Python IO streams in examples Python IO streams: BytesIO and StringIO in practice. We’ll be using Python IO streams: BytesIO and StringIO to execute some tasks from the real world: sending a photo …
🌐
Wellsr
wellsr.com › python › basics › python-io-input-output-examples
Python I/O: Input and Output Examples and Tools - wellsr.com
August 31, 2018 - This tutorial describes Python input and output (I/O) from the terminal, using the os module, manually importing text files, using with statements, and the csv module.
🌐
GeeksforGeeks
geeksforgeeks.org › python › optimized-i-o-operations-in-python
Optimized I/O Operations in Python - GeeksforGeeks
July 23, 2025 - Python is widely used for file input/output (I/O) operations due to its simplicity and versatility. However, when working with large files or numerous Input/Output operations, optimizing file I/O becomes vital for efficient performance.
🌐
Medium
medium.com › @sanyamdubey28 › file-handling-and-working-with-the-io-package-in-python-ac2ab5c25215
File Handling and Working with the io Package in Python | by Sanyamdubey | Medium
May 2, 2025 - File handling is a fundamental aspect of programming, enabling developers to read from and write to files efficiently. In Python, the built-in io module provides a robust framework for handling file operations, offering tools to work with files ...
🌐
Python
docs.python.org › 3.1 › library › io.html
15.2. io — Core tools for working with streams — Python v3.1.5 documentation
September 4, 2012 - The io module provides Python’s main facilities for dealing for various types of I/O. There are three main types of I/O: text I/O, binary I/O, raw I/O. These are generic categories, and various backing stores can be used for each of them.
🌐
KnowledgeHut
knowledgehut.com › https://www.knowledgehut.com › tutorials › programming tutorials
Python FileIO Tutorial | Read & Write Files in Python
Learn Python FileIO to read, write, and manage files efficiently. Step-by-step Python tutorial for beginners and developers.