Search notes:

Python standard library: io

dir(io)

import io
for e in sorted(dir(io), key=lambda s: s.lower()): print(e)
__all__
__author__
__builtins__
__cached__
__doc__
__file__
__getattr__
__loader__
__name__
__package__
__spec__
_io
_WindowsConsoleIO
abc
BlockingIOError
BufferedIOBase
BufferedRandom
BufferedReader
BufferedRWPair
BufferedWriter
BytesIO
DEFAULT_BUFFER_SIZE
FileIO
IncrementalNewlineDecoder
IOBase
open
open_code
RawIOBase
SEEK_CUR
SEEK_END
SEEK_SET
StringIO The StringIO class creates a «file-like» object from a string, which allows to read from it as if it were a file. Compare with csv.StringIO.
text_encoding
TextIOBase
TextIOWrapper
UnsupportedOperation

File objects

Python deals with three kinds of file objects.
The interfaces for these interfaces are defined in the io module.
See also the open function.

StringIO

In Python, strings are immutable. So they cannot be modified in-place.
However, with the io module, such an in-place modification can be simulated with StringIO():
import io

txt = 'Hello world!'

txt_io = io.StringIO(txt)

print(txt_io.getvalue())

txt_io.seek(6)
txt_io.write('there')

print(txt_io.getvalue())
Github repository about-Python, path: /standard-library/io/StringIO.py
This example is from the Python FAQ.

open

io.open() is the same function as the built-in function open().

Slurp content of file into a variable

The content of an entire file can easily be read into a variable with the following construct. __file__ needs to be replaced with the filename to be read:
with open (__file__, 'r') as f:
     text = f.read()

print(text)

See also

standard library

Index