Skip to content
Question

Q.Differentiate between "w" and "r" file modes used in Python. Illustrate the difference using suitable examples.

CBSECBSE Class XII Board 2020Subjective· 2mImportance★★★★★
🔒 Locked · start free trial →

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 →

The "w" mode opens a file for writing (overwriting existing content or creating a new file), while "r" mode opens a file for reading only — attempting to write in "r" mode will raise an error.

When you work with files in Python, the first decision you make is how you want to interact with that file. Do you want to read what's already there? Or do you want to write new data into it? This choice is expressed through the file mode — a string you pass to the open() function. The two most fundamental modes are "r" (read) and "w" (write). They are opposites in purpose, and using the wrong one can either lose your data or crash your program.

Let's start with "r" mode. This is the default mode if you don't specify anything. When you open a file in read mode, Python assumes the file already exists and that you only want to look at its contents. You can call methods like .read(), .readline(), or iterate over the file object line by line. But if you try to write — say, by calling .write("hello") — Python will raise an io.UnsupportedOperation error. The file is opened in read-only mode, and the operating system enforces that.

Watch out

If the file you try to open with "r" does not exist, Python raises a FileNotFoundError. The program stops. So "r" is strict: the file must be there.

Now consider "w" mode. This is the write mode, and it behaves very differently. When you open a file with "w", Python does two things: if the file already exists, it truncates it — that is, it deletes all existing content and starts fresh. If the file does not exist, Python creates a new empty file for you. Then you can write data into it using .write(). But you cannot read from a file opened in "w" mode — trying to call .read() will give an error.

Important

The "w" mode is destructive. Opening an existing file in "w" mode erases its contents immediately, even before you write anything. There is no "undo". Always be certain before using "w".

Let's see this with examples. Suppose you have a file called notes.txt that contains the line "Original content."

Example 1: Using "r" mode

file = open("notes.txt", "r")
content = file.read()
print(content)   # Output: Original content.
file.close()

This works fine. But if you try:

file = open("notes.txt", "r")
file.write("New text")   # Error! UnsupportedOperation: not writable

Python refuses because the mode is read-only.

Example 2: Using "w" mode

file = open("notes.txt", "w")
file.write("New content.")
file.close()

After this code runs, notes.txt no longer has "Original content." It now contains only "New content." The old data is gone.

If you try to read from a file opened in "w" mode:

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.