Thursday, December 26, 2024
Google search engine
HomeLanguagesStringIO Module in Python

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




# 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 neveropen.")
 
# 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())


Output:

This is initial string. 
The string after writing is: This is initial string. Welcome to neveropen.

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




# Importing the StringIO module.
from io import StringIO 
 
 
# The arbitrary string.
string ='Hello and welcome to GeeksForGeeks.'
 
# Using the StringIO method to
# set as file object.
file = StringIO(string)
 
# Retrieve the entire content of the file.
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




# Importing the StringIO module.
from io import StringIO 
 
 
# The arbitrary string.
string ='Hello and welcome to GeeksForGeeks.'
 
# Using the StringIO method to
# set as file object.
file = StringIO(string)
 
# This will returns whether the file
# is interactive or not.
print("Is the file stream interactive?", file.isatty())
 
# This will returns whether the file is
# readable or not.
print("Is the file stream readable?", file.readable())
 
# This will returns whether the file supports
# writing or not.
print("Is the file stream writable?", file.writable())
 
# This will returns whether the file is
# seekable or not.
print("Is the file stream seekable?", file.seekable())
 
# This will returns whether the file is
# closed or not.
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




# Importing the StringIO module.
from io import StringIO 
 
 
# The arbitrary string.
string ='Hello and welcome to GeeksForGeeks.'
 
# Using the StringIO method
# to set as file object.
file = StringIO(string)
 
# Reading the file:
print(file.read())
 
# Now if we again want to read
# the file it shows empty file
# because the cursor is set to
# the last index.
 
 
# This does not print anything
# because the function returns an
# empty string.
print(file.read())
 
# Hence to set the cursor position
# to read or write the file again
# we use seek().We can pass any index
# here from(0 to len(file))
file.seek(0)
 
# Now we can able to read the file again()
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




# Importing the StringIO module.
from io import StringIO 
 
 
# The arbitrary string.
string ='Hello and welcome to GeeksForGeeks.'
 
# Using the StringIO method
# to set as file object.
file = StringIO(string)
 
# Reading the initial file:
print(file.read())
 
# To set the cursor at 0.
file.seek(0)
 
 
# This will drop the file after
# index 18.
file.truncate(18)
 
# File after truncate.
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




# Importing the StringIO module.
from io import StringIO 
 
 
# The arbitrary string.
string ='Hello and welcome to GeeksForGeeks.'
 
# Using the StringIO method to
# set as file object.
file = StringIO(string)
 
# Here the cursor is at index 0.
print(file.tell())
 
# Cursor is set to index 20.
file.seek(20)
 
# Print the index of cursor
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




# Importing the StringIO module.
from io import StringIO 
 
 
# The arbitrary string.
string ='Hello and welcome to GeeksForGeeks.'
 
# Using the StringIO method to
# set as file object.
file = StringIO(string)
 
# Reading the file.
print(file.read())
 
# Closing the file.
file.close()
 
# If we now perform any operation on the file
# it will raise an ValueError.
 
# This is to know whether the
# file is closed or not.
print("Is the file closed?", file.closed)


Output: 

Hello and welcome to GeeksForGeeks.
Is the file closed? True

RELATED ARTICLES

Most Popular

Recent Comments