Tutorials  /  Python

Python io - BytesIO and StringIO

Ccentron Redaktion · February 2024 ·4 min read ·Python, Tutorial

Python io module allows us to manage the file-related input and output operations. The advantage of using the IO module is that the classes and functions available allows us to extend the functionality to enable writing to the Unicode data.

Python IO Module

There are many ways in which we can use the io module to perform stream and buffer operations in Python. We will demonstrate a lot of examples here to prove the point. Let’s get started.

Python BytesIO

Just like what we do with variables, data can be kept as bytes in an in-memory buffer when we use the io module’s Byte IO operations. Here is a sample program to demonstrate this:

Python
import io
stream_str = io.BytesIO(b"JournalDev Python: \x00\x01")
print(stream_str.getvalue())

Let’s see the output for this program:

Code
sh-3.2$ python io_bytes.py
JournalDev Python:
sh-3.2$

The getvalue() function just takes the value from the Buffer as a String.

Python StringIO

We can even use StringIO as well which is extremely similar in use to BytesIO. Here is a sample program:

Python
import io

data = io.StringIO()
data.write('JournalDev: ')
print('Python.', file=data)

print(data.getvalue())

data.close()

Let’s see the output for this program:

Python io string example

Notice that we even closed the buffer after we’re done with the buffer. This helps save buffer memory as they store data in-memory. Also, we used the print method with an optional argument to specify an IO stream of the variable, which is perfectly compatible with a print statement.

Reading using StringIO

Once we write some data to the StringIO buffer, we can read it as well. Let’s look at a code snippet:

Python
import io

input = io.StringIO('This goes into the read buffer.')
print(input.read())

Let’s see the output for this program:

Python io string read example

Reading file using StringIO

It is also possible to read a file and stream it over a network as Bytes. The io module can be used to convert a media file like an image to be converted to bytes. Here is a sample program:

Python
import io

file = io.open("whale.png", "rb", buffering = 0)
print(file.read())

Let’s see the output for this program:

Python io media
VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

io.open() vs os.open()

The io.open() function is a much preferred way to perform I/O operations as it is made as a high-level interface to peform file I/O. It wraps the OS-level file descriptor in an object which we can use to access the file in a Pythonic way. The os.open() function takes care of the lower-level POSIX syscall. It takes input POSIX based arguments and returns a file descriptor which represents the opened file. It does not return a file object; the returned value will not have read() or write() functions. Overall, io.open() function is just a wrapper over os.open() function. The os.open() function just also sets default config like flags and mode too while io.open() doesn’t to it and depends on the values passed to it.

Conclusion - BytesIO and StringIO

In this lesson, we studied simple operations of python IO module and how we can manage the Unicode characters with BytesIO as well.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren