os and io
Host filesystem access, available when PK_ENABLE_OS=1.
Files
open(path, mode) constructs an io.FileIO. Both arguments are required.
Use modes such as 'r', 'w', 'a', 'rb', and 'wb'.
Binary modes ending in b read/write bytes; text modes read/write strings.
import os
file = open('example.txt', 'w')
try:
assert file.write('hello') == 5
except Exception:
file.close()
raise
file.close()
file = open('example.txt', 'r')
try:
text = file.read()
except Exception:
file.close()
raise
file.close()
assert text == 'hello'
assert os.path.exists('example.txt')
os.remove('example.txt')
io.SEEK_SET, io.SEEK_CUR, and io.SEEK_END select the origin for
seek. There are no encoding/error-policy arguments, StringIO,
BytesIO, or full CPython text-stream interfaces.
Close files explicitly on both normal and exceptional paths. The context manager limitations also apply to file objects.
OS functions
Availability of individual OS operations depends on the target. The module does not provide CPython's full path utilities or directory APIs. For virtual filesystems or packaged scripts, configure host import callbacks.
Implementation: os.c.