Skip to content
Exercises · Q7

Q.Write a Python program to open the file hello.txt used in question no 6 in read mode to display its contents. What will be the difference if the file was opened in write mode instead of append mode?

Yanam BieapTextbookSubjective· 3mImportance★★★★★
50% · 11/22 Questions
🔒 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 →

This question tests your understanding of Python file modes — specifically the difference between read, write, and append modes. The core idea is that each mode determines what operations are allowed on the file and what happens to existing content when the file is opened.

The Idea: File Handling Methods

File handling in Python is about controlling access to data stored on disk. The mode you choose when opening a file is like setting the rules of engagement — it decides whether you can read, write, or both, and crucially, whether existing content is preserved or destroyed.

The three modes in this question are:

  • Read mode ('r') — you can only look at the content, never change it
  • Write mode ('w') — you can write new content, but it erases everything that was there before
  • Append mode ('a') — you can add new content to the end of the file, keeping what was already there

This distinction between write and append is one of the most common sources of accidental data loss in beginner programs.

The Program

# Opening hello.txt in read mode to display its contents
with open("hello.txt", "r") as file:
    content = file.read()
    print("Contents of hello.txt:")
    print(content)

Expected output (assuming hello.txt contains "Hello, World!"):

Contents of hello.txt:
Hello, World!

Key lines explained

  • open("hello.txt", "r") — the "r" mode opens the file for reading only. If the file doesn't exist, Python raises a FileNotFoundError. The file pointer starts at the beginning.
  • file.read() — reads the entire content of the file as a single string. For large files, you might use file.readline() or iterate line by line.
  • with statement — this is a context manager that automatically closes the file when the block ends, even if an error occurs. This is the correct way to handle files in Python.
Tip

Always use the with statement for file operations. It guarantees the file is closed properly, preventing resource leaks and corrupted data.

The Difference: Write Mode vs Append Mode

Now for the critical part of the question — what changes if you use write mode instead of append mode?

Write mode ('w')

with open("hello.txt", "w") as file:
    file.write("New content")
  • Erases all existing content in the file the moment you open it
  • Creates a new file if one doesn't exist
  • The file pointer starts at the beginning
  • After writing, the file contains only what you just wrote

Append mode ('a')

with open("hello.txt", "a") as file:
    file.write("New content")
  • Preserves all existing content
  • Creates a new file if one doesn't exist (same as write mode)
  • The file pointer starts at the end of the file
  • After writing, the file contains old content + new content at the end …

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.