Input/Output
Files, streams, buffering, and encodings.
9 questions
JuniorTheoryVery commonWhat do open() modes r, w, x, a, t, b, + mean?
What do open() modes r, w, x, a, t, b, + mean?
r reads; w writes and truncates any existing file; x creates exclusively and fails if it exists; a appends. A second letter sets type: t text (default) or b binary. + opens for both read and write.
Common mistakes
- ✗Using
wand losing the file's contents because it truncates on open - ✗Expecting
xto open an existing file instead of failing when it exists - ✗Thinking
awrites from the start rather than appending at the end
Follow-up questions
- →What does combining
w+versusr+change about the initial file state? - →Why does
xmode help avoid races when creating a lock file?
JuniorTheoryCommonWhat is a file object in Python?
What is a file object in Python?
An object exposing a file-like API (read(), write(), close(), iteration) over a resource: a disk file, in-memory buffer, pipe, or socket. Also called a stream, it is a context manager for with.
Common mistakes
- ✗Assuming a file object always wraps a physical disk file rather than any stream
- ✗Forgetting that file objects are context managers usable with
with - ✗Confusing the file object with the path string passed to
open()
Follow-up questions
- →What does iterating over a text file object yield on each step?
- →How does
with open(...)guarantee the file is closed on an exception?
JuniorTheoryCommonHow do text and binary file modes differ?
How do text and binary file modes differ?
Text mode reads and writes str, applying an encoding plus newline translation. Binary mode ('b') reads and writes raw bytes/bytearray with no encoding and no newline change — exactly the bytes on disk.
Common mistakes
- ✗Opening binary data like images in text mode, corrupting it via decoding
- ✗Expecting
read()in binary mode to return astrinstead ofbytes - ✗Forgetting that text mode translates newlines and applies an encoding
Follow-up questions
- →Which
open()parameter sets the encoding used in text mode? - →Why must network protocols and image files be read in binary mode?
MiddleTheoryCommonHow do json and pickle serialization differ?
How do json and pickle serialization differ?
json produces human-readable text, is cross-language, but handles only basic types (dict, list, str, num, bool, None). pickle produces Python-specific binary and serializes almost any Python object, but is not portable across languages.
Common mistakes
- ✗Trying to
json.dumpsa custom object or set without a custom encoder - ✗Expecting
pickleoutput to be readable or parseable by other languages - ✗Using
picklefor data that crosses a trust boundary or language barrier
Follow-up questions
- →Which Python types does
jsonmap to and from out of the box? - →Why is
picklefaster thanjsonfor large nested Python objects?
MiddleTheoryOccasionalWhy must you close files, and how does buffering relate?
Why must you close files, and how does buffering relate?
Buffered writes sit in memory and reach disk only when the buffer fills or on flush()/close(), so not closing can lose data and hold a lock. close() flushes and frees the OS handle; with guarantees it. GC closes only eventually.
Common mistakes
- ✗Assuming writes are persisted immediately and skipping
close()orwith - ✗Believing
close()releases the handle without flushing buffered data - ✗Relying on garbage collection to close files at a predictable time
Follow-up questions
- →When would you call
flush()explicitly instead of waiting forclose()? - →What does the
bufferingargument toopen()control?
MiddleTheoryOccasionalWhat are io.StringIO and io.BytesIO for?
What are io.StringIO and io.BytesIO for?
In-memory stream objects with the same file API: io.StringIO holds text (str), io.BytesIO holds bytes. They let code expecting a file work on memory — handy for tests, capturing output, or building data without disk.
Common mistakes
- ✗Writing
bytestoio.StringIOorstrtoio.BytesIOand getting a type error - ✗Thinking they are backed by disk rather than living purely in memory
- ✗Forgetting to
seek(0)before reading back what was just written
Follow-up questions
- →How do you retrieve the full accumulated content with
getvalue()? - →Why is
io.StringIOuseful for redirecting and capturingprintoutput?
MiddleTheoryOccasionalWhat do a file object's tell() and seek() methods do?
What do a file object's tell() and seek() methods do?
tell() returns the current position in the stream; seek(offset, whence) moves it, where whence is 0 (start), 1 (current) or 2 (end). In binary mode you position freely by byte; in text mode offset must be a value returned by tell(), since encodings make raw byte math unsafe.
Common mistakes
- ✗Assuming
tell()/seek()count in lines rather than bytes - ✗Doing arithmetic on text-mode positions instead of reusing a
tell()value - ✗Forgetting
seek(0)rewinds the stream so the file can be re-read
Follow-up questions
- →Why is byte-offset arithmetic unsafe on a text-mode file but fine in binary mode?
- →What does
seek(0, 2)followed bytell()tell you about the file?
SeniorTheoryRareHow do you JSON-serialize an object json doesn't support out of the box?
How do you JSON-serialize an object json doesn't support out of the box?
json handles only dict, list, str, numbers, bool and None. For anything else — a set, Decimal, or custom class — pass default=func to dumps; it is called per unsupported object and must return a serializable value. Or subclass JSONEncoder and override default. On load, object_hook rebuilds types.
Common mistakes
- ✗Expecting
jsonto serializedatetime,set, or custom classes without help - ✗Returning a non-serializable value from
defaultinstead of a built-in type - ✗Forgetting that decoding needs
object_hookto rebuild the original type
Follow-up questions
- →When would you reach for
pickleinstead of a customjsonencoder? - →How does
defaultdecide which objects it is called for?
SeniorTheoryRareWhy is unpickling untrusted data a security risk?
Why is unpickling untrusted data a security risk?
pickle is not secure: loading a pickle can run arbitrary code via an object's __reduce__, so unpickling untrusted data can execute attacker-controlled code. Never unpickle untrusted input; prefer json or a signed format.
Common mistakes
- ✗Unpickling network or user-supplied data without any signing or validation
- ✗Assuming the binary format alone prevents code execution during loading
- ✗Believing
jsonis equally dangerous and so seeing no reason to switch
Follow-up questions
- →How does an object's
__reduce__enable code execution during unpickling? - →What signing or HMAC scheme would make a pickle payload safer to load?