StringIO Module in Python
The StringIO module is an in-memory file-like object. This object can be used as input or output to the most function that would expect a standard file object. When the StringIO object is created it is initialized by passing a string to the constructor. If no string is passed the StringIO will start empty. In both cases, the initial cursor on the file starts at zero.
NOTE: This module does not exist in the latest version of Python so to work with this module we have to import it from the io module in Python as io.StringIO.
Example:
Python3
from io import StringIO
string = 'This is initial string.'
file = StringIO(string)
print ( file .read())
file .write( " Welcome to geeksforgeeks." )
file .seek( 0 )
print ( 'The string after writing is:' , file .read())
|
Output:
This is initial string.
The string after writing is: This is initial string. Welcome to geeksforgeeks.
Some of the important methods of StringIO are as follow:
1. StringIO.getvalue(): This function returns the entire content of the file.
Syntax:
File_name.getvalue()
Example:
Python3
from io import StringIO
string = 'Hello and welcome to GeeksForGeeks.'
file = StringIO(string)
print ( file .getvalue())
|
Output:
'Hello and welcome to GeeksForGeeks.'
2. In this we discuss about some functions of StringIO which returns Boolean values i.e either True or false:
- StringIO.isatty():This function Return True if the stream is interactive and False if the stream not is interactive
- StringIO.readable():This function return True if the file is readable and returns False if file is not readable.
- StringIO.writable():This function return True if the file supports writing and returns False if file does not support writing.
- StringIO.seekable():This function return True if the file supports random access and returns False if file does not support random access.
- StringIO.closed:This function return True if the file is closed and returns False if file is open.
Syntax:
File_name.isatty()
File_name.readable()
File_name.writable()
File_name.seekable()
File_name.closed
Example:
Python3
from io import StringIO
string = 'Hello and welcome to GeeksForGeeks.'
file = StringIO(string)
print ( "Is the file stream interactive?" , file .isatty())
print ( "Is the file stream readable?" , file .readable())
print ( "Is the file stream writable?" , file .writable())
print ( "Is the file stream seekable?" , file .seekable())
print ( "Is the file closed?" , file .closed)
|
Output:
Is the file stream interactive? False
Is the file stream readable? True
Is the file stream writable? True
Is the file stream seekable? True
Is the file closed? False
3.StringIO.seek(): The seek() function is used set the cursor position on the file. If we perform any read and write operation on a file the cursor is set on the last index so to move the cursor at starting index of the file seek() is used.
Syntax:
File_name.seek(argument)
# Here the argument is passed to tell the
# function where to set the cursor position.
Example:
Python3
from io import StringIO
string = 'Hello and welcome to GeeksForGeeks.'
file = StringIO(string)
print ( file .read())
print ( file .read())
file .seek( 0 )
print ( file .read())
|
Output:
Hello and welcome to GeeksForGeeks.
Hello and welcome to GeeksForGeeks.
4.StringIO.truncate(): This function is used to resize the size of the file stream. This method drops the file after the provided index and saves it.
Syntax:
File_name.truncate(size = None)
# We can provide the size from where
# to truncate the file.
Example:
Python3
from io import StringIO
string = 'Hello and welcome to GeeksForGeeks.'
file = StringIO(string)
print ( file .read())
file .seek( 0 )
file .truncate( 18 )
print ( file .read())
|
Output:
Hello and welcome to GeeksForGeeks.
Hello and welcome
5.StringIO.tell(): This method is used to tell the current stream or cursor position of the file.
Syntax:
File_name.tell()
Example:
Python3
from io import StringIO
string = 'Hello and welcome to GeeksForGeeks.'
file = StringIO(string)
print ( file .tell())
file .seek( 20 )
print ( file .tell())
|
Output:
0
20
6.StringIO.close(): This method is used to close the file. After this function called on a file, we cannot perform any operation on the file. If any operation is performed, it will raise a ValueError.
Syntax:
File_name.close()
Example:
Python3
from io import StringIO
string = 'Hello and welcome to GeeksForGeeks.'
file = StringIO(string)
print ( file .read())
file .close()
print ( "Is the file closed?" , file .closed)
|
Output:
Hello and welcome to GeeksForGeeks.
Is the file closed? True
Last Updated :
23 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...