Engineering Python · Functions & Files
Reading and Writing Text
In Engineering Python because labs exchange notes and small tables as text — open/read/write patterns matter even when the browser has no host disk.
Desktop Python uses `open` to read and write files. In this browser runtime there is no real host filesystem for your laptop's folders — labs use in-memory strings or `io.StringIO` to practice the same read/write ideas honestly.
- Engineering Python
- Medium level
- 4 concepts
1open / read / write / close
On a real machine: `with open(path) as f:` reads or writes and closes reliably. Mode `'r'` reads text; `'w'` writes (overwrites).
Figure. On a real machine, with open(path) as f: reads or writes and closes reliably. Mode 'r' reads text; 'w' writes (overwrites). The desktop pattern writes hello to notes.txt and reads it back.
Desktop open
# Desktop pattern (not run as-is in the browser lab):
# with open('notes.txt', 'w') as f:
# f.write('hello\n')
# with open('notes.txt') as f:
# print(f.read())Why prefer `with open(...) as f:` on a desktop?
- It closes the file even if an error occurs in the block
- It disables Unicode
- It makes Python faster always
Context managers close resources reliably.
2In-browser: no host filesystem
The in-browser Python runtime does not expose your laptop's files. Pretending `open('/Users/...')` works would be a lie.
We practice with `io.StringIO` — a file-like object backed by a string — so read/write calls feel the same.
Figure. The in-browser Python runtime does not expose your laptop's files. Pretending open('/Users/...') works would be a lie. Practice with io.StringIO — a file-like object backed by a string.
Takeaway
- IdeaThe in-browser Python runtime does not expose your laptop's files. Pretending `open('/Users/...')` works would be a lie.
3StringIO as a file-like object
`StringIO` supports `.write`, `.getvalue`, and `.seek` so you can rehearse file APIs without a disk.
Figure. StringIO supports .write, .getvalue, and .seek so you can rehearse file APIs without a disk. Two writes put alpha then beta in the buffer; getvalue() reads them back.
Takeaway
- Idea`StringIO` supports `.write`, `.getvalue`, and `.seek` so you can rehearse file APIs without a disk.
StringIO
import io
buf = io.StringIO()
buf.write('alpha\n')
buf.write('beta\n')
print(buf.getvalue())4Lab: write and read via StringIO
Write two lines into a StringIO and print the buffer contents.
Figure. Write row1 and row2 into a StringIO (with newlines), then print getvalue(). The buffer holds both lines; the check can see row1.
Takeaway
- IdeaWrite two lines into a StringIO and print the buffer contents.
Coding lab. In-memory text buffer runs in the app, with checks on your output.
Notes
- In Engineering Python because labs exchange notes and small tables as text — open/read/write patterns matter even when the browser has no host disk.
- On a real machine: `with open(path) as f:` reads or writes and closes reliably. Mode `'r'` reads text; `'w'` writes (overwrites).
- The in-browser Python runtime does not expose your laptop's files. Pretending `open('/Users/...')` works would be a lie.
Exam traps & shortcuts
- Run one cell at a time and read stdout before changing more lines.
- Names are labels for values; rebinding a name does not rewrite old prints.
Recap
Desktop uses open/with; browser labs use StringIO; the read/write ideas transfer.
- open / read / write / close
- On a real machine: `with open(path) as f:` reads or writes and closes reliably. Mode `'r'` reads text; `'w'` writes (overwrites).
- In-browser: no host filesystem
- The in-browser Python runtime does not expose your laptop's files. Pretending `open('/Users/...')` works would be a lie.
- StringIO as a file-like object
- `StringIO` supports `.write`, `.getvalue`, and `.seek` so you can rehearse file APIs without a disk.
- Lab: write and read via StringIO
- Write two lines into a StringIO and print the buffer contents.
Practise Reading and Writing Text
Reading is free and needs no account. Practice, mocks and progress live in the app.
- A 2-question practice set that ends the chapter
- 1 quick check with worked explanations
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device