Append Mode
Completed
Write start to log.txt. Then read n events and append each one on its own line using mode "a". Finally print the number of lines in the file and its last line.
Input
Line 1: n. Then n events.
Output
Two lines.
Example
Input
2 login logout
Output
3 logout
Editor
Python
Hints
"a" adds to the end instead of overwriting.
read().splitlines() gives a list of lines without \n.
Solution explanation
Mode "w" truncates, "a" appends, "r" (the default) reads.
Show solution
with open("log.txt", "w") as f:
f.write("start\n")
for _ in range(int(input())):
with open("log.txt", "a") as f:
f.write(input() + "\n")
with open("log.txt") as f:
lines = f.read().splitlines()
print(len(lines))
print(lines[-1])