io module provides interfaces to stream handling. open. 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 |
io module. open function. 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())
io.open() is the same function as the built-in function open(). with open(__file__, 'r') as f:
print_type(f)
with open(__file__, 'rb') as f:
print_type(f)
with open('/tmp/a', 'w' ) as f:
print_type(f)
with open('/tmp/a', 'wb') as f:
print_type(f)
_io.TextIOWrapper _io.TextIOWrapper <- _io._TextIOBase <- _io._IOBase <- builtins.object _io.BufferedReader _io.BufferedReader <- _io._BufferedIOBase <- _io._IOBase <- builtins.object _io.TextIOWrapper _io.TextIOWrapper <- _io._TextIOBase <- _io._IOBase <- builtins.object _io.BufferedWriter _io.BufferedWriter <- _io._BufferedIOBase <- _io._IOBase <- builtins.object
__file__ needs to be replaced with the filename to be read: with open (__file__, 'r') as f:
text = f.read()
print(text)