Io stringio Python
Python programming language:
Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way. The python language can also be used in web development; Django and Flask are the frameworks used to create web applications using Python. In Python, indentation is the main concept; if we do not follow proper indentation, then the program will not run properly, and we will get an error in the output. Python programming language contains methods or functions to reduce the size of the code, and the python programming language provides built-in functions and user-defined functions. We can import the functions in the python programming language through the libraries, which can be downloaded using the python package manager ( pip ). While working on the project and we want to develop the project using the python programming language. The python programming language makes our work easy by providing built-in functions, with these imported using the # import. The import statement is used to impost the modules or built-in functions into the program so we can develop the project efficiently and faster. Python programming language is an object-oriented and high-level language it is easier to learn when compared to other programming languages.
The python programming language contains mainly six built-in datatypes; these six data types help solve the problem efficiently and faster. The python programming language consists of a built-in function and provides libraries and modules that can be imported to solve the problem more easily. Generally, there are many versions of python interpreters available. Still, from them, we need to download the version of Python more significantly than or equal to 3.4 so that the code runs faster and we can observe the output in the console. An object that resembles a file in memory is the StringIO module. The majority of functions that would anticipate a standard file object can accept this object as input or output. The function Object() { [native code] } of the StringIO object receives a string as input when it is constructed, which initialises the object. The StringIO will begin empty if no string is given. The starting cursor on the file starts at zero in both situations.
StringIo module:
An object that resembles a file in memory is the StringIO module. The majority of functions that would anticipate a standard file object can accept this object as input or output. The function Object() { [native code] } of the StringIO object receives a string as input when it is constructed, which initialises the object. The StringIO will begin empty if no string is given. The starting cursor on the file starts at zero in both situations.
NOTE: The newest version of Python does not contain this module, thus in order to use it, we must import it from the io module as io.StringIO.
Example:
# importing the module stringIO from the inputoutput module
from io import StringIO
# Creating the variable data to store the arbitrary data
data ='Starting string'
# Creating the variable file_data
file_data = StringIO(data)
# now we need to read the data in the file using read( ) function
print(file_data.read())
# We can also write the data into the file using the write( ) function
file.write(" Welcome to python programming language.")
#Now with the help of the seek( ) function we can point our cursor to the required index
file_data.seek(0)
# Now we need to print the data present in the file_data
print(' string after writing data is:', file.read())
Output:
Starting string.
String after writing data is: Starting string is. Welcome to python programming language.
The following list includes some of StringIO's key methods:
1. The function StringIO.getvalue() returns the whole contents of the file.
Syntax:
File_name.getvalue()
Parameters:
File_name: We need to provide the required file name in which we need to provide the required file name in which we need to write the data.
Example:
# importing the module stringIO from the inputoutput module
from io import StringIO
# Creating the variable data to store the arbitrary data
data ='Welcome to python programming language'
# Creating the variable file_data
file_data = StringIO(data)
# Printing the entire content of the file.
print(file_data.getvalue())
Output:
Welcome to python programming language
2. Here, we describe some StringIO operations that produce Boolean results, or either True or False:
StringIO.isatty( ):
This function, StringIO.isatty(), returns True if the stream is interactive and False otherwise
.
StringIO.readable():
If the file can be read, this function returns True; otherwise, it returns False.
StringIO.writable( ):
When a file can be written to, the function StringIO.writable() returns True; otherwise, it returns False.
StringIO.seekable():
If the file enables random access, this method will return True; otherwise, it will return False.
StringIO.closed( ):
When a file is closed, the function StringIO.closed returns True; otherwise, it returns False.
Syntax:
Name_file.isatty()
Name_file.readable()
Name_file.writable()
Name_file.seekable()
Name_file.closed
Parameters:
Name_file: The name of the file in which we need to perform the operations.
Example:
# importing the module stringIO from the inputoutput module
from io import StringIO
# Creating the arbitrary variable data to store the input data
data ='Welcome to Python programming language.'
# Now with the help of the StringIO function input the data into the file_data
file_data = StringIO(data)
# Now with the help of the isatty( ) function we can find weather the given file is interactive or not.
print("Weather the file is interactive or not?", file_data.isatty())
# Now with the help of the readable( ) function we can find weather the given file is readable or not
print("Weather the file is readable or not", file_data.readable())
# Now with the help of the writeable( ) function we can find weather the given file is writeable or not
print("Weather the file is writable or not ?", file_data.writable())
# Now with the help of the seekable( ) function we can find weather the given file is seekable or not
print("Weather the function is seekable or not?", file_data.seekable())
# Now with the help of the closed( ) function we can find weather the given file is closed or not
print("weather the file is closed or not?", file_data.closed)
Output:
Weather the file is interactive or not? False
Weather the file is readable or not? True
Weather the file is stream writable or not? True
Weather the file stream seekable or not? True
Weather the file is closed or not? False
3. StringIO.seek():
Use the seek() method of StringIO to place the file cursor. When we read or write to a file, the cursor is set at the last index; hence, seek() is used to move the cursor to the file's starting index.
Syntax:
Name_file.seek(argument)
Parameters:
Name_file: The name of the file in which we need to perform the operations.
argument: In argument we need to pass the required index of the column in which we need to read the given data
4.StringIO.truncate():
The file stream's size can be changed using this function. This technique saves the file after dropping the file following the supplied index.
Syntax:
Name_file.truncate(size = None)
Parameters:
Name_file: The name of the file in which we need to perform the operations.
Size: We can provide the size parameter from where we need to truncate the file
Example:
# importing the module stringIO from the inputoutput module
from io import StringIO
# Creating the arbitrary variable data to store the input data
data ='Welcome to Python programming language.'
# Now with the help of the StringIO function input the data into the file_data
file_data = StringIO(data)
# With the help of the read( ) function read the original file and print the data
print(file_data.read())
# With the help of the seek( ) function we can obtain the data present in index 0
file_data.seek(0)
# Now when we set the truncate index to 12 it will set the file from the index 12
File_data.truncate(12)
# Finaaly reading the data after we perform the truncate operation
print(file_data.read())
Output:
Welcome to python programming language
Welcome to
5.StringIO.close():
The file is closed using this method. We are unable to operate on a file after this function has been called on it. Any operation will result in a ValueError being raised.
Syntax:
Name_file.close()
Example:
# importing the module stringIO from the inputoutput module
from io import StringIO
# Creating the arbitrary variable data to store the input data
data ='Welcome to Python programming language.'
# Now with the help of the StringIO function input the data into the file_data
file_data = StringIO(data)
# With the help of the read( ) function read the original file and print the data
print(file_data.read())
# With the help of the close( ) function read the original file and print the data
file.close()
print("Weather the file is closed or not?", file.closed)
Output:
Welcome python programming language.
Weather the file is closed? True