RUNTIME VALUE CONVERSIONS
Use an update mode to read and write through one UTF-8 handle, with an explicit seek whenever you switch direction.
Select Transpile → Runtime. Use a direct with open(..., encoding="utf-8") statement. These modes are a bounded Runtime profile. Successful seek is required between reading and writing, in either direction. Conversion itself never opens your source program's files.
| Mode | On opening | Where writes go |
|---|---|---|
"r+" | Open an existing file at its beginning; fail if missing. Preserve its contents. | At the established cursor, overwriting bytes without truncating the remainder. |
"w+" | Create a file, or immediately truncate an existing file to empty. | At the established cursor. |
"a+" | Create if missing; preserve existing contents. Start at EOF. | Always at EOF, even after seeking elsewhere. |
"x+" | Create only if absent; otherwise raise FileExistsError without modifying the existing file. | At the established cursor. |
All four modes admit reads, line iteration, writes, writelines, tell, seek, flush, and close. Mode, encoding, errors, and newline options must be literals. Supported newlines are None and ""; decoding remains strict UTF-8.
This example creates or overwrites update-demo.txt, writes two Unicode lines, then rewinds before reading them back.
def main():
with open("update-demo.txt", "w+", encoding="utf-8", newline="") as f:
f.writelines(["café\n", "ready\n"])
f.seek(0)
print(f.readline().strip())
print(f.readline().strip())
return 0
Expected output:
café ready
Read an existing file, then seek to zero before replacing its contents. This example replaces one ASCII digit with another, keeping the encoded byte length unchanged. It creates or overwrites status-demo.txt to make the input reproducible.
def main():
with open("status-demo.txt", "w", encoding="utf-8", newline="") as f:
f.write("status=0\ncafé\n")
with open("status-demo.txt", "r+", encoding="utf-8", newline="") as f:
text = f.read()
f.seek(0)
f.write(text.replace("status=0", "status=1"))
f.seek(0)
print(f.readline().strip())
print(f.readline().strip())
return 0
Expected output:
status=1 café
Use seek(0) or seek(0, 2) to switch from reading to writing. Nonzero bookmarks taken during reading replay reads only in this profile. Python's decoder cookies can include buffered newline state that changes a subsequent write position. Bookmarks taken after writing can establish a later write position, provided that position has not also been recorded during reading. See File bookmarks and exclusive creation for replay and iteration rules.
An append-update handle starts at EOF. Seek to the beginning to inspect existing content. A later write still appends, regardless of the seek target. This example resets append-demo.txt before opening it in append-update mode.
def main():
with open("append-demo.txt", "w", encoding="utf-8", newline="") as f:
f.write("first\n")
with open("append-demo.txt", "a+", encoding="utf-8", newline="") as f:
f.write("second\n")
f.seek(0)
print(f.read().splitlines())
return 0
Expected output:
['first', 'second']
Opening w+ truncates immediately. Exclusive x+ protects an existing destination but does not roll back a later partial write. Use Exceptions and cleanup for recovery and File-processing limits for resource policies.