Challenge - 5 Problems
File Append Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Appending text to a file
What will be the content of the file
Assume
example.txt after running this code?Assume
example.txt initially contains the text: HelloPython
with open('example.txt', 'a') as f: f.write(' World')
Attempts:
2 left
💡 Hint
Appending adds text at the end without removing existing content.
✗ Incorrect
The mode 'a' opens the file for appending. The write adds ' World' right after 'Hello' without adding a new line.
❓ Predict Output
intermediate2:00remaining
Appending with newline character
What will be the content of
Assume
log.txt after running this code?Assume
log.txt initially contains:StartPython
with open('log.txt', 'a') as file: file.write('\nEnd')
Attempts:
2 left
💡 Hint
The '\n' adds a new line before 'End'.
✗ Incorrect
The '\n' character creates a new line, so 'End' appears on the line after 'Start'.
❓ Predict Output
advanced2:30remaining
Appending binary data to a file
What will be the output of this code snippet?
Assume
Assume
data.bin initially contains bytes: b'\x01\x02'Python
with open('data.bin', 'ab') as f: f.write(b'\x03\x04') with open('data.bin', 'rb') as f: content = f.read() print(content)
Attempts:
2 left
💡 Hint
Appending in binary mode adds bytes at the end.
✗ Incorrect
Opening with 'ab' appends bytes to the file. Reading after shows original plus appended bytes.
❓ Predict Output
advanced2:30remaining
Appending text with encoding
What will be the content of
Assume
unicode.txt after running this code?Assume
unicode.txt initially contains: caféPython
with open('unicode.txt', 'a', encoding='utf-8') as f: f.write(' ☕') with open('unicode.txt', encoding='utf-8') as f: content = f.read() print(content)
Attempts:
2 left
💡 Hint
Appending adds the coffee cup emoji with a space before it.
✗ Incorrect
The write adds a space and the emoji after the existing text. Reading shows combined text.
🧠 Conceptual
expert2:00remaining
File pointer position after appending
After opening a file in append mode ('a'), what is the position of the file pointer before writing?
Choose the correct statement.
Choose the correct statement.
Attempts:
2 left
💡 Hint
Append mode means adding data after existing content.
✗ Incorrect
In append mode, the file pointer is always moved to the end before each write, so data is added after existing content.