Q.Write a statement in Python to open a text file WRITEUP.TXT so that new content can be written in it.
🔒You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
🔒 Start your 14-day free trial to unlock the full solution →Part (a)Concept understanding — File Access Modes
Imagine you have a physical diary or a notebook. You can do different things with it: you can open it to write new entries, you can open it just to read what you already wrote, or you can open it to add a new page at the end. You might also want to open it in a way that prevents anyone else from writing in it while you are reading. These different ways of "opening" the notebook are exactly what File Access Modes are for a computer file.
A file access mode is simply the permission you request from the operating system when you tell it you want to work with a file. It is a rule that says, "I want to open this file, and here is exactly what I plan to do with it." The operating system then checks if you are allowed to do that, and if you are, it sets up the file accordingly.
The most fundamental modes are three, and they mirror your notebook perfectly:
- Read Mode (
r): You can only look at the contents. You cannot change a single character. This is like opening your notebook to study for an exam — you read, but you do not write. If the file does not exist, the operating system will tell you it cannot find it (an error). - Write Mode (
w): You can write new content, but here is the critical point — it starts from a blank page. Opening a file in write mode is like tearing out all the existing pages of your notebook and then writing fresh. It destroys whatever was there before. If the file does not exist, the system will create a brand new empty notebook for you. - Append Mode (
a): You can write new content, but only at the very end of the file. This is like adding a new page at the back of your notebook. Everything you wrote earlier stays untouched. If the file does not exist, the system creates a new one, just like in write mode.
The key difference between Write and Append is preservation. Write erases the past; Append protects it. If you are logging daily sales data, you would use Append so yesterday's numbers are not lost. If you are creating a new report from scratch, you use Write.
There is also a very common fourth mode that combines reading and writing: …
Part (b)Concept understanding — File Access Modes
Imagine you have a physical diary or a notebook. You can do different things with it: you can open it to write new entries, you can open it just to read what you already wrote, or you can open it to add a new page at the end. You might also want to open it in a way that prevents anyone else from writing in it while you are reading. These different ways of "opening" the notebook are exactly what File Access Modes are for a computer file.
A file access mode is simply the permission you request from the operating system when you tell it you want to work with a file. It is a rule that says, "I want to open this file, and here is exactly what I plan to do with it." The operating system then checks if you are allowed to do that, and if you are, it sets up the file accordingly.
The most fundamental modes are three, and they mirror your notebook perfectly:
- Read Mode (
r): You can only look at the contents. You cannot change a single character. This is like opening your notebook to study for an exam — you read, but you do not write. If the file does not exist, the operating system will tell you it cannot find it (an error). - Write Mode (
w): You can write new content, but here is the critical point — it starts from a blank page. Opening a file in write mode is like tearing out all the existing pages of your notebook and then writing fresh. It destroys whatever was there before. If the file does not exist, the system will create a brand new empty notebook for you. - Append Mode (
a): You can write new content, but only at the very end of the file. This is like adding a new page at the back of your notebook. Everything you wrote earlier stays untouched. If the file does not exist, the system creates a new one, just like in write mode.
The key difference between Write and Append is preservation. Write erases the past; Append protects it. If you are logging daily sales data, you would use Append so yesterday's numbers are not lost. If you are creating a new report from scratch, you use Write.
There is also a very common fourth mode that combines reading and writing: …
Part (a)
Open WRITEUP.TXT in write mode ("w") so new content can be written. Mode "w" creates the file if it does not exist and truncates it if it does.
f = open("WRITEUP.TXT", "w") …
Part (a): open for writing with open("WRITEUP.TXT", "w").
Part (b): open for reading with open("README.TXT", "r").
Part (a)
To write new content into a text file you use write mode, "w". If the file does not exist Python creates it; if it does exist, its contents are truncated (erased) before writing begins.
f = open("WRITEUP.TXT", "w")
``` …
- CBSE 2026Set 91/41 markMCQQ.Consider the statement given below : f1 = open("pqr.dat","________") Which of the following is the correct file mode to open the file in read only mode ? (A) a (B) rb (C) r+ (D) rb+
›Reveal solutionSolution
To open a file in read-only mode, the correct file mode is
"r"— but among the given options,"rb"is the only one that opens a file for reading (in binary mode), making it the correct choice.When you work with files in Python, the mode string you pass to the
open()function determines exactly what you can do with that file — read, write, append, or a combination. The question asks specifically for read-only mode, which means the file should be opened only for reading, not for writing or appending.Let's look at each option:
-
(A)
"a"— This stands for append mode. It opens the file for writing, but data is added at the end of the file. It is not read-only at all. In fact, if the file does not exist,"a"creates a new one. So this is incorrect. -
(B)
"rb"— The"r"means read, and the"b"means binary mode. Together,"rb"opens the file for reading in binary format. This is indeed a read-only mode — you cannot write to the file. Among the given options, this is the correct one. -
(C)
"r+"— The"+"adds both reading and writing. So"r+"opens the file for both reading and writing. It is not read-only; you can modify the file. Hence, it is incorrect for the given requirement. -
(D)
"rb+"— This combines binary mode with read-write access. Like"r+", it allows both reading and writing, just in binary format. So it is also not read-only. …
-
- CBSE 2023Set 91/11 markMCQQ.Which of the following mode keeps the file offset position at the end of the file?(a) r+(b) r(c) w(d) a
›Reveal solutionSolution
Mode a (append) keeps the file offset position at the end of the file, ensuring all writes happen after existing content.
When working with files in programming, the mode you choose determines not just whether you can read or write, but also where the file pointer starts and how the existing content is treated. Understanding these distinctions is essential for handling data correctly.
The r mode opens a file for reading only. The file pointer sits at the beginning, ready to read from the start. You cannot write in this mode, and if the file doesn't exist, you'll get an error. It's purely for consumption of existing data.
The r+ mode opens a file for both reading and writing. The file pointer again starts at the beginning. This means if you write immediately, you'll overwrite existing content from the start unless you explicitly move the pointer. The file must already exist, or the operation fails. It's a two-way mode but begins at the top.
The w mode opens a file for writing. Here's the crucial behavior: it truncates the file, meaning any existing content is wiped out the moment you open it. The file pointer is at the beginning of what is now an empty (or newly created) file. If the file didn't exist, it gets created. This mode is destructive by design. …
- CBSE 2023Set 91/41 markMCQQ.Which of the following modes in Python creates a new file, if file does not exist and overwrites the content, if the file exists ?(a) r+(b) r(c) w(d) a
›Reveal solutionSolution
The
'w'(write) mode in Python creates a new file if it doesn't exist and overwrites all existing content if the file already does.When working with files in Python, we often need to specify how we intend to interact with the file. This is done through what are called "modes" when opening a file. These modes dictate whether you want to read from the file, write to it, append new content, or a combination of these actions, and also define how the system should behave if the file doesn't exist or already contains data. Understanding these modes is fundamental to effective file handling.
Let's look at the common file modes:
-
'r' (Read Mode):
This is the default mode. When you open a file in read mode, you can only read its contents. You cannot modify or add new data.
Watch outIf the file you are trying to open in
'r'mode does not exist, Python will raise aFileNotFoundError. It will never create a new file in this mode. -
'r+' (Read and Write Mode):
This mode allows you to both read from and write to a file. The file pointer is initially at the beginning of the file.
ImportantSimilar to
'r'mode, if the file does not exist when opened in'r+'mode, Python will raise aFileNotFoundError. It does not create a new file. If the file exists, writing will occur from the current position of the file pointer, potentially overwriting existing content from that point onwards. -
'w' (Write Mode):
This mode is specifically for writing data to a file. It has a very distinct behavior regarding file existence:
- If the file does not exist, Python will create a new, empty file with the specified name. …
-
- CBSE 2019Set 91/41 markQ.Write a statement in Python to open a text file WRITEUP.TXT so that new content can be written in it.(OR)Write a statement in Python to open a text file README.TXT so that existing content can be read from it.
›Reveal solutionSolution
Part (a): open for writing with
open("WRITEUP.TXT", "w").Part (b): open for reading with
open("README.TXT", "r").Part (a)
To write new content into a text file you use write mode,
"w". If the file does not exist Python creates it; if it does exist, its contents are truncated (erased) before writing begins.f = open("WRITEUP.TXT", "w") ``` …
🎓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.