Challenge - 5 Problems
File System Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Reading a file's content
What is the output of this code if the file
example.txt contains the text Hello World?Python
with open('example.txt', 'r') as f: content = f.read() print(content)
Attempts:
2 left
💡 Hint
The
read() method returns the whole file content as a string.✗ Incorrect
The open function with mode 'r' opens the file for reading as text. The read() method reads the entire content as a string. So the output is the exact text inside the file.
❓ Predict Output
intermediate2:00remaining
Writing and reading back a file
What will be printed after running this code?
Python
with open('testfile.txt', 'w') as f: f.write('12345') with open('testfile.txt', 'r') as f: print(f.readline())
Attempts:
2 left
💡 Hint
The
write method writes text, and readline reads one line as a string.✗ Incorrect
The file is written with the string 12345. Then reading one line returns the whole string since there is no newline. So it prints 12345.
❓ Predict Output
advanced2:00remaining
Using
os.path to check file existenceWhat does this code print if the file
data.txt does NOT exist in the current folder?Python
import os if os.path.exists('data.txt'): print('Found') else: print('Not Found')
Attempts:
2 left
💡 Hint
The
os.path.exists function returns True if the file exists, otherwise False.✗ Incorrect
Since the file does not exist, os.path.exists returns False, so the else branch prints Not Found.
❓ Predict Output
advanced2:00remaining
Understanding file modes and errors
What error does this code raise if
readonly.txt exists but is read-only?Python
with open('readonly.txt', 'w') as f: f.write('Trying to write')
Attempts:
2 left
💡 Hint
Opening a read-only file for writing usually causes a permission error.
✗ Incorrect
Trying to open a file with write mode 'w' when the file is read-only causes a PermissionError because the program lacks permission to modify the file.
🧠 Conceptual
expert2:00remaining
File pointer position after reading
After running this code, what is the value of
pos?Python
with open('numbers.txt', 'w') as f: f.write('1234567890') with open('numbers.txt', 'r') as f: f.read(4) pos = f.tell() print(pos)
Attempts:
2 left
💡 Hint
The
tell() method returns the current position of the file pointer.✗ Incorrect
The read(4) reads 4 characters, moving the file pointer 4 positions forward. So tell() returns 4.