How to emulate file opened in text mode in Python
You can use BytesIO
instead:
import io
s = "αβγδεζηθικλμνξoπρστυφχψω"
with io.BytesIO(s.encode("utf-8")) as f:
f.seek(8)
print(f.read(8).decode("utf-8"))
You can use BytesIO
and TextIOWrapper
to emulate the behavior of a real file:
text = 'αβγδεζηθικλμνξoπρστυφχψω'
with io.BytesIO(text.encode('utf8')) as binary_file:
with io.TextIOWrapper(binary_file, encoding='utf8') as file_obj:
file_obj.seek(8)
print(file_obj.read(8))
# εζηθικλμ