Summary

ref: link and stackoverflow

  1. StringIO and BytesIO are methods(objects) that manipulate string and bytes data in memory(RAM).
  2. StringIO is used for string data and BytesIO is used for binary data.
  3. This classes create file like object() that operate on string data.
  4. The StringIO and BytesIO classes are most useful in scenarios where you need to mimic a normal file.

Example

import io
s = io.StringIO()
print(s.write("Hello World/n"))
# ------->Output: 13
# adding to the memory buffer using print statement
print("adding using the print",file = s)
# get all of the data written in the file
print(s.getvalue())
# ------>Output: Hello World/nadding using the print
# wrapping a file interface around a string
s = io.StringIO("Hello\nWorld\n")
print(s.readlines())
# ------>Output: ['Hello\n', 'World\n']
# StringIO class should be only used for strings
# if we are dealing with bytes we should use BytesIO
s = io.BytesIO()
s.write(b"This is a binary string")
print(s.getbuffer())
print(s.getvalue())

In this case the data won’t be kept in the memory(RAM) after it’s written to the file using

with open("test.bin","wb") as f:
    f.write(b"Hello world")
    f.write(b"Hello world")
    f.write(b"Hello world")
    f.write(b"Hello world")
    f.write(b"Hello world")

In this case instead of writing contents to a file, it is written to a memory buffer.(a chunk of ram)

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

essentially writing using BytesIO class is equivalent to

space = b""
space += b"Hello world"
space += b"Hello world"
space += b"Hello world"
space += b"Hello world"
space += b"Hello world"

Then why we define io.BytesIO() and why not use the bytes concatenation ? Ans: Optimization and Performance

# then why not simply use the above mentioned.
# optimization and performance
import io
import time

start = time.time()
buffer = b""
for i in range(0,90000):
    buffer += b"Hello World"
end = time.time()
total = end - start
print(total)

start = time.time()
buffer = io.BytesIO()
for i in range(0,90000):
    buffer.write(b"Hello World")
end = time.time()
total = end - start
print(total)

"""
1.8411164283752441
0.01295328140258789
"""