Home » StringIO Module in Python

StringIO Module in Python

by Online Tutorials Library

StringIO Module in Python

It is the StringIO module is an in-memory, file-like object. It can be used to input or output the majority of functions users can expect from an ordinary file object. Once the user creates the StringIO objects, it is initially created by providing a string to the constructor. If there is no string, the StringIO will be empty. In both instances, the initially displayed cursor on the file will start at zero.

The module is not available in the most recent version of Python; thus, to be able to use this module, we need to transfer it into the Io module in Python in the form of io.StringIO.

Example:

Output:

This is the initialized string.  The file of the string after writing in it is: This is the initialized string. Welcome to  tutoraspire.com.  

Important Methods of StringIO:

Following are some methods of StringIO:

1. StringIO.getvalue(): This function is used for returning the entire content of the file.

Syntax:

Syntax of the above method is:

Example:

Output:

Hello and thank you for visiting to  tutoraspire.com  

2. In this, we look at some of the functions of StringIO that return a Boolean value, i.e., either false or true:

  1. isatty(): This function of StringIO is used for returning False if the stream is not interactive and True if the stream is interactive.
  2. readable(): This function of StringIO is used for returning False if the file is not readable and True if the file is readable.
  3. writable(): This function of StringIO is used for returning False if the file does not support writing and True if the file supports writing.
  4. seekable(): This function of StringIO is used for returning False if the file does not support random access and True if the file supports random access.
  5. closed: This function of StringIO is used for returning False in case the file is open and it returns True if the file is closed.

Syntax:

Syntaxes of the above method is:

Example:

Output:

Is the file stream above interactive? False  Is the file stream above readable? True  Is the file stream above writable True  Is the file stream above seekable? True  Is the file above closed? False  

3. StringIO.seek(): The seek() function is used to set the cursor’s position within the file. If we execute any write or read operation on a document, the cursor is placed on the index that was last used so that we can move the cursor from the beginning position of the file seek() is employed.

Syntax:

Syntax of the above method is:

Example:

Output:

Hello and thank you for visiting to  tutoraspire.com.    Hello and thank you for visiting to  tutoraspire.com.  

4. StringIO.truncate(): This function is used for resizing the size of the file stream. This method saves the file and drops it after the given index.

Syntax:

Syntaxes of the above method is:

Example:

Output:

Hello and welcome to  tutoraspire.com.  Hello and welc  

5. StringIO.tell(): This method is used for telling the file’s current stream and cursor position.

Syntax:

Syntaxes of the above method is:

Example:

Output:

0  23  

6. StringIO.close() This is used for closing the file. This function is called on a file, and we can’t perform any operations on it. Any operation that is done will result in a ValueError.

Syntax: =

Syntaxes of the above method is:

Example:

Output:

Hello and welcome to  tutoraspire.com.  Is the file closed? True  

Next Topic#

You may also like