StringIO Module in Python
StringIO is a module in Python that allows you to treat strings as file-like objects. It provides an in-memory stream for text I/O (input/output), which means you can read from and write to a string just like you would with a file, but without disk I/O. To use StringIO, you need to import it from the io module:
from io import StringIO
Example:
from io import StringIO
s = 'This is initial string.'
f = StringIO(s) # StringIO object
print(f.read())
f.write(" Welcome to GeeksForGeeks.")
f.seek(0)
print(f.read())
Output
This is initial string. This is initial string. Welcome to GeeksForGeeks.
Explanation:
- f.read() reads from the current cursor to the end, moving the cursor to the end after reading.
- f.write(" Welcome to GeeksForGeeks.") appends the new string at the current cursor position (end of the stream).
- f.seek(0) moves the cursor back to the beginning of the stream.
- f.read() reads the full content from the start, printing the combined text.
StringIO methods and functions
1. getvalue(): This method returns the entire contents of the StringIO object as a single string. It is helpful when you want to retrieve all the text you’ve written to the stream.
from io import StringIO
f = StringIO("Hello and welcome to GeeksForGeeks.")
print(f.getvalue())
Output
Hello and welcome to GeeksForGeeks.
Explanation: This code creates a StringIO text stream initialized with a string and prints its entire content using getvalue().
2. Boolean utility functions: These are functions that returns either True or False. They are used to check specific properties or conditions of an object like readability, writability, etc. Let’s understand the following boolean utility functions with the help of the table below:
Function | Description | Returns |
---|---|---|
isatty() | Checks if the stream is interactive | False (always) |
readable() | Checks if the stream can be read | True |
writable() | Checks if the stream supports writing | True |
seekable() | Checks if the stream allows moving the cursor | True |
closed | Checks if the stream is closed | True or False |
from io import StringIO
f = StringIO("Sample")
print(f.isatty())
print(f.readable())
print(f.writable())
print(f.seekable())
print(f.closed)
Output
False True True True False
Explanation: This code creates a StringIO text stream and uses boolean functions to check and print whether the stream is interactive, readable, writable, seekable or closed.
3. seek(position): This method allows you to move the internal cursor to a specific position within the stream. It is necessary if you want to read or write from a particular point.
from io import StringIO
f = StringIO("Hello")
print(f.read())
print(f.read())
f.seek(0)
print(f.read())
Output
Hello Hello
Explanation: This code creates a StringIO stream with "Hello", reads and prints it. The second read prints nothing as the cursor is at the end. After seek(0), it reads and prints the content again.
4. truncate(size=None): This method resizes the stream. If a size is provided, the stream is cut off after that many characters. If no size is provided, it truncates the stream at the current cursor position.
from io import StringIO
f = StringIO("Hello and welcome")
f.seek(0)
f.truncate(10)
f.seek(0)
print(f.read())
Output
Hello and
Explanation: This code creates a StringIO stream, truncates its content to 10 characters, resets the cursor and prints the truncated content.
5. tell(): This method returns the current position of the cursor in the stream. It helps you keep track of where you are reading or writing.
from io import StringIO
f = StringIO("Python")
print(f.tell())
f.read(3)
print(f.tell())
Output
0 3
Explanation: This code creates a StringIO stream with "Python" and prints the initial cursor position (0). It then reads 3 characters, moves the cursor and prints the new position (3).
6. close(): This method closes the stream. Once the stream is closed, you can no longer read from or write to it and attempting to do so will raise an error.
from io import StringIO
f = StringIO("Python")
f.close()
print(f.closed)
Output
True
Explanation: This code creates a StringIO stream with "Python", closes the stream and then prints whether the stream is closed .