The IO module is very useful when you want to perform file related I / O (like reading / writing files).
Although you can use normal read () methods
and write ()
to read or write to a file, but this module gives us a lot more flexibility regarding these operations.
This module is part of the standard library, so there is no need to install it separately using pip.
To import the io module, we can do the following:
import io
There are 2 generic classes in the io
module that are very useful:
- BytesIO -" I / O operations with byte data;
- StringIO -" string I / O.
We can access these classes using io.BytesIO
and io.StringIO
.
Python BytesIO class
Here we can store our data as bytes ( b’’
). When we use io.BytesIO
, the data is stored in a buffer in memory.
We can get an instance of the byte stream using the constructor:
import io bytes_stream = io.BytesIO (b’Hello from Journaldevx0AHow are you? ’)
Note that we are passing a byte string (with the b
).
Right now bytes_stream
&it’s just a descriptor for a byte stream.
To print data inside a buffer, we need to use bytes_stream.getvalue ()
.
import io bytes_stream = io.BytesIO (b’Hello from Journaldevx0AHow are you? ’) print (bytes_stream.getvalue ())
Here getvalue ()
takes the value of the byte string from the descriptor.
Since the byte string x0A
is the ASCII representation of a newline (& # 8216; n & # 8217;), we get the following output:
Exit
b’Hello from Journaldev How are you? ’
It is now always recommended to close the buffer handle after how we finished our work. This is also necessary in order to free all the memory allocated for the buffer.
To close the buffer, use:
bytes_stream.close ()
Python StringIO class
The io.StringIO
class can read string-related data from a StringIO buffer.
import io string_stream = io.StringIO ("Hello from Journaldev How are you?")
We can read from a string buffer with string_stream.read ()
and write with string_stream.write ()
.
We can print the content with getvalue ( )
.
import io string_stream = io.StringIO ("Hello from Journaldev How ar e you?") # Print old content of buffer print (f’Initially, buffer: {string_stream.getvalue ()} ’) # Write to the StringIO buffer string_stream.write (’ This will overwrite the old content of the buffer if the length of this string exceeds the old content’) prin t (f’Finally, buffer: {string_stream.getvalue ()} ’) # Close the buffer string_stream.close ()
Exit
Initially, buffer: Hello from Journaldev How are you? Finally, buffer: This will overwrite the old content of the buffer if the length of this string exceeds the old content
Since we are writing to the same buffer, the new content will obviously overwrite the old.
Reading from the StringIO buffer
Similar to writing, we can also read from the buffer.read ()
buffer using buffer.read ()
.
import io input = io.StringIO (’This goes into the read buffer.’) print (input.read ())
Exit
This goes into the read buffer.
As you can see, the content is now inside the read buffer, which is printed with buffer.read ()
.
Reading a file with io
We can also use the io.open ()
method to read directly from a file, similar to reading from a file object.
Here, this module gives us the choice between buffered and unbuffered reads.
For example, the following would use buffered reads to read a file by setting buffering = SIZE
. If SIZE
= 0, there will be no buffering.
Suppose sample.txt
has the following content:
Hello from JournalDev! How are you? This is the last line.
import io # Read from a text file in binary format using io.open () # We read / write using a buffer size of 5 bytes file = io.open ("sample.txt", "rb", buffering = 5) print (file.read ()) # Close the file file.close ()
Exit
b’Hello from JournalDev! How are you? This is the last line. ’
As you can see, the file was read successfully. Here io
will read the file using a buffer of approximately 5 bytes.
Comparison of io.open () and os.open ()
Function io.open ( ) &the most preferred way of doing I / O because it is implemented as a high-level Pythonic interface.
In contrast, os.open ()
will make a system call to the open () function
. This will return a file descriptor that cannot be used as a io
descriptor object.
Since io.open ()
is a wrapper function for os. open ()
, it is generally recommended to use such wrapper functions as they automatically handle many errors for you.