Python StringIO allows using an in-memory string as a file object.
The object can be used as input or output for functions that expect a file object
io.StringIO
when you need to write text
io.BytesIO
when you need to write data
You will have to manually handle positioning in the “file” - e.g. with the seek()
method.
Example:
# Importing the StringIO module.
from io import StringIO
# The arbitrary string.
string ='This is initial string.'
# Using the StringIO method to set as file object.
# Now we have an object file that we will able to treat just like a file.
file = StringIO(string)
# this will read the file
print(file.read())
# We can also write this file.
file.write(" Welcome to geeksforgeeks.")
# This will make the cursor at index 0.
file.seek(0)
# This will print the file after writing in the initial string.
print('The string after writing is:', file.read())