0
0
Pythonprogramming~20 mins

Appending data to files in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Append Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Appending text to a file
What will be the content of the file example.txt after running this code?

Assume example.txt initially contains the text: Hello
Python
with open('example.txt', 'a') as f:
    f.write(' World')
AHello
BHello\nWorld
C World
DHello World
Attempts:
2 left
💡 Hint
Appending adds text at the end without removing existing content.
Predict Output
intermediate
2:00remaining
Appending with newline character
What will be the content of log.txt after running this code?

Assume log.txt initially contains:
Start
Python
with open('log.txt', 'a') as file:
    file.write('\nEnd')
AStartEnd
B\nEnd
CStart\nEnd
DStart\n\nEnd
Attempts:
2 left
💡 Hint
The '\n' adds a new line before 'End'.
Predict Output
advanced
2:30remaining
Appending binary data to a file
What will be the output of this code snippet?

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)
Ab'\x01\x02\x03\x04'
Bb'\x03\x04'
Cb'\x01\x02'
Db''
Attempts:
2 left
💡 Hint
Appending in binary mode adds bytes at the end.
Predict Output
advanced
2:30remaining
Appending text with encoding
What will be the content of 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)
Acafé ☕
Bcafé☕
Ccafé \u2615
Dcafé
Attempts:
2 left
💡 Hint
Appending adds the coffee cup emoji with a space before it.
🧠 Conceptual
expert
2: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.
AThe file pointer is at a random position, so writes may overwrite or append.
BThe file pointer is at the end of the file, so writes always add data after existing content.
CThe file pointer is at the beginning, so writes overwrite existing content.
DThe file pointer is at the middle of the file, so writes insert data there.
Attempts:
2 left
💡 Hint
Append mode means adding data after existing content.