A. Missing parentheses after read, so content is a method, not string.
B. File is not opened in read mode.
C. File is not closed after reading.
D. print() cannot print file content.
Solution
Step 1: Check method call syntax
The code uses file.read without parentheses, so it assigns the method itself, not the result of reading.
Step 2: Understand effect on print
Printing content prints a method object reference, not file text, causing confusion.
Final Answer:
Missing parentheses after read, so content is a method, not string. -> Option A
Quick Check:
Always call read() with parentheses to get content [OK]
Hint: Add () after read to get content, not method [OK]
Common Mistakes:
Forgetting parentheses on read()
Ignoring file close (less critical here)
Assuming print can't show file content
5. You want to read the entire content of a file and count how many times the word "python" appears, ignoring case. Which code snippet correctly does this?
hard
A. file = open('file.txt')
text = file.readlines()
count = text.count('python')
file.close()
print(count)
B. with open('file.txt') as f:
text = f.read()
count = text.lower().count('python')
print(count)
C. with open('file.txt') as f:
count = 0
for line in f:
if 'python' in line:
count += 1
print(count)
D. with open('file.txt') as f:
text = f.read()
count = text.count('python')
print(count)
Solution
Step 1: Read entire file content
Using with open() and f.read() reads all text at once safely.
Step 2: Count occurrences ignoring case
Convert text to lowercase with text.lower() then count 'python' to ignore case differences.
Step 3: Verify other options
with open('file.txt') as f:
count = 0
for line in f:
if 'python' in line:
count += 1
print(count) counts lines containing 'python' but misses multiple occurrences per line and case sensitivity. file = open('file.txt')
text = file.readlines()
count = text.count('python')
file.close()
print(count) misuses count() on list of lines. with open('file.txt') as f:
text = f.read()
count = text.count('python')
print(count) counts only exact case matches.
Final Answer:
with open('file.txt') as f:
text = f.read()
count = text.lower().count('python')
print(count) -> Option B
Quick Check:
Use read() + lower() + count() for case-insensitive word count [OK]
Hint: Lowercase text before count() to ignore case [OK]
Common Mistakes:
Counting lines instead of all occurrences
Not converting text to lowercase
Using count() on list instead of string
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.