All I could find is this statement:
The easiest way to create a text stream is with open(), optionally specifying an encoding:
f = open("myfile.txt", "r", encoding="utf-8")
In-memory text streams are also available as StringIO objects:
f = io.StringIO("some initial text data")
But this gives no insight at all on when I should use open()
over io.StringIO
and vice-versa. I know that they do not work exactly the same behind the scene. But why would someone go for open()
in Python 3 ?
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.
StringIO and BytesIO are methods that manipulate string and bytes data in memory. StringIO is used for string data and BytesIO is used for binary data. This classes create file like object that operate on string data. The StringIO and BytesIO classes are most useful in scenarios where you need to mimic a normal file.
StringIO gives you file-like access to strings, so you can use an existing module that deals with a file and change almost nothing and make it work with strings. For example, say you have a logger that writes things to a file and you want to instead send the log output over the network.
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.
The difference is: open
takes a file name (and some other arguments like mode or encoding), io.StringIO
takes a plain string and both return file-like objects.
Hence:
open
to read files ;StringIO
when you need a file-like object and you want to pass the content of a string.An example with StringIO
:
import csv
import io
reader = csv.reader(io.StringIO("a,b,c\n1,2,3"))
print ([r for r in reader])
# output [['a', 'b', 'c'], ['1', '2', '3']]
It's very useful because you can use a string where a file was expected.
In the usual case, with a csv file on your disk, you would write something like:
with open(<path/to/file.csv>, ...) as f:
reader = csv.reader(f, ...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With