Q.Write the file mode that will be used for opening the following files. Also, write the Python statements to open the following files:
Choosing the correct file mode string ('r', 'w', 'a', 'b', '+') determines how Python opens a file — whether it reads, writes, appends, treats content as text or binary, and whether it allows both operations simultaneously.
Why file modes matter
When you open a file in Python, the mode string tells the operating system and the interpreter exactly what you intend to do. The wrong mode can overwrite data you meant to preserve ('w' truncates), fail to read when you need to ('a' is write-only), or misinterpret binary data as text. Each mode is a contract: it grants specific permissions and enforces specific behaviors.
The mode string is built from these characters:
'r'— read: file must exist; pointer starts at the beginning'w'— write: creates file if absent, truncates to zero length if present'a'— append: creates file if absent, pointer jumps to end; all writes land at EOF regardless of seeks'b'— binary: no encoding/decoding, no newline translation; omit it (or use't') for text'+'— update: allows both read and write on the same handle
You combine them: 'rb' is read-binary, 'w+' is write-plus-read (truncates first), 'a+' is append-plus-read.
Solutions for each file
(a) Text file "example.txt" in both read and write mode
Mode: 'r+'
The 'r+' mode opens an existing file for reading and writing without truncating it. The file pointer starts at the beginning, so you can read first, then seek and write, or write immediately (which overwrites from position 0 unless you seek). The file must already exist; if it doesn't, Python raises FileNotFoundError.
f = open('example.txt', 'r+')
'r+' does not create the file if it is missing. If you need to create it when absent, use 'w+' (but that truncates) or 'a+' (append-read, pointer at end).
(b) Binary file "bfile.dat" in write mode
Mode: 'wb'
The 'w' opens for writing (creates or truncates), and 'b' marks it binary. Any existing content is discarded the moment you open. You write bytes objects (b'...' or bytearray) rather than strings.
f = open('bfile.dat', 'wb')
A common use case is saving serialized data (pickle, struct-packed records, images). Remember to close the file or use a context manager so the OS flushes the buffer.
(c) Text file "try.txt" in append and read mode
Mode: 'a+'
The 'a' opens for appending (creates if absent, never truncates), and '+' adds read capability. The write pointer is locked at EOF — every write() lands at the end no matter where you seek. The read pointer, however, starts at the beginning, so you can read the entire file and then append new lines.
f = open('try.txt', 'a+')
After opening with 'a+', call f.seek(0) before reading if you want to see existing content from the start. Otherwise the read pointer sits at EOF and read() returns an empty string.
(d) Binary file "btry.dat" in read only mode
Mode: 'rb'
The 'r' is read-only, 'b' is binary. The file must exist. You get bytes back from read(), not strings, and no newline translation occurs.
f = open('btry.dat', 'rb')
This is the mode for reading images, audio, pickled objects, or any non-text data where every byte matters.
Summary table
| File | Mode | Purpose | Creates? | Truncates? | Pointer start |
|---|---|---|---|---|---|
| example.txt | 'r+' | Read and write (text) | No | No | Beginning |
| bfile.dat | 'wb' | Write (binary) | Yes | Yes | Beginning |
| try.txt | 'a+' | Append and read (text) | Yes | No | End (write), Beginning (read) |
| btry.dat | 'rb' | Read only (binary) | No | No | Beginning |
(a) 'r+', open('example.txt', 'r+') — (b) 'wb', open('bfile.dat', 'wb') — (c) 'a+', open('try.txt', 'a+') — (d) 'rb', open('btry.dat', 'rb').
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.