Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
File modes and access types
📖 Scenario: You are working on a simple program that saves and reads notes from a text file. You will learn how to open files in different modes to write and read data.
🎯 Goal: Build a program that creates a file, writes a note, reads the note back, and shows the content on the screen using correct file modes.
📋 What You'll Learn
Create a file and write text to it using the correct file mode
Open the file to read the text back
Use the correct file modes for writing and reading
Print the content read from the file
💡 Why This Matters
🌍 Real World
Saving and reading notes or logs is common in many apps like diaries, to-do lists, or data logging.
💼 Career
Understanding file modes is essential for jobs involving data processing, automation, and software development.
Progress0 / 4 steps
1
Create and open a file in write mode
Create a variable called file and open a file named notes.txt in write mode using open('notes.txt', 'w').
Python
Hint
Use 'w' mode to open a file for writing. This will create the file if it does not exist.
2
Write a note to the file
Use the write method on the file variable to write the string 'My first note.' to the file.
Python
Hint
Use file.write('My first note.') to add text to the file.
3
Close the file and reopen in read mode
Close the file using file.close(). Then create a new variable called file and open notes.txt in read mode using open('notes.txt', 'r').
Python
Hint
Always close a file after writing. Use file.close(). Then open it again with 'r' mode to read.
4
Read and print the file content
Read the content of the file using file.read() and print it using print(). Then close the file.
Python
Hint
Use content = file.read() to get the text, then print(content) to show it.
Practice
(1/5)
1. Which file mode in Python opens a file for reading only, and raises an error if the file does not exist? file = open('data.txt', mode)
easy
A. "r"
B. "w"
C. "a"
D. "x"
Solution
Step 1: Understand the purpose of mode "r"
Mode "r" opens a file for reading only and requires the file to exist.
Step 2: Compare with other modes
Mode "w" opens for writing (creates or truncates), "a" appends, and "x" creates a new file but errors if it exists.
Final Answer:
"r" -> Option A
Quick Check:
Read-only mode = "r" [OK]
Hint: Read-only mode is just "r" [OK]
Common Mistakes:
Confusing "r" with "w" which overwrites files
Using "a" thinking it reads
Choosing "x" which creates files
2. Which of the following is the correct syntax to open a file named log.txt for appending text in Python?
easy
A. open('log.txt', 'r')
B. open('log.txt', 'w')
C. open('log.txt', 'a')
D. open('log.txt', 'x')
Solution
Step 1: Identify the mode for appending
Mode "a" opens the file for appending, adding new content at the end.
Step 2: Check syntax correctness
The syntax open('log.txt', 'a') is correct for appending text.
Final Answer:
open('log.txt', 'a') -> Option C
Quick Check:
Append mode = "a" [OK]
Hint: Append mode is always "a" [OK]
Common Mistakes:
Using "r" which is read-only
Using "w" which overwrites the file
Using "x" which creates new file only
3. What will be the output of the following code if example.txt contains the text "Hello"?
with open('example.txt', 'w') as f:
f.write('World')
with open('example.txt', 'r') as f:
print(f.read())
medium
A. World
B. Hello
C. HelloWorld
D. Error
Solution
Step 1: Understand mode "w" effect on file content
Opening with "w" overwrites the file, so "World" replaces "Hello".
Step 2: Reading the file after writing
Reading the file after writing will output the new content "World".
Final Answer:
World -> Option A
Quick Check:
Write mode overwrites content = "World" [OK]
Hint: Write mode "w" replaces file content [OK]
Common Mistakes:
Assuming "w" appends instead of overwrites
Expecting original content to remain
Thinking reading causes error after writing
4. What is wrong with this code snippet?
f = open('data.txt', 'r')
content = f.read()
f.write('More data')
f.close()
medium
A. No error, code is correct
B. File is not closed properly
C. File mode should be "a" to read
D. File is opened in read mode but write is attempted
Solution
Step 1: Check file mode and operations
The file is opened in "r" (read) mode, which does not allow writing.
Step 2: Identify the error cause
Calling f.write() in read mode causes a runtime error because writing is not allowed.
Final Answer:
File is opened in read mode but write is attempted -> Option D
Quick Check:
Write not allowed in "r" mode [OK]
Hint: Write needs "w" or "a" mode, not "r" [OK]
Common Mistakes:
Forgetting write is disallowed in read mode
Not closing file properly (though here it is closed)
Confusing append mode with read mode
5. You want to create a new file report.txt but only if it does not already exist. Which mode should you use to avoid overwriting existing files?
hard
A. "w"
B. "x"
C. "a"
D. "r+"
Solution
Step 1: Understand mode "x" behavior
Mode "x" creates a new file and raises an error if the file already exists, preventing overwriting.
Step 2: Compare with other modes
"w" overwrites, "a" appends or creates, "r+" opens for reading and writing but requires file to exist.
Final Answer:
"x" -> Option B
Quick Check:
Create new only = "x" mode [OK]
Hint: Use "x" to create file only if not exists [OK]