Appending to a file

Recall the several modes which are supported for working with files:
Mode
Operation
Example
'r'
Read (the default one)
with open('document.txt', 'r') as f:
'w'
Write
with open('document.txt', 'w') as f:
'a'
Append
with open('document.txt', 'a') as f:
'r+'
Read + Write
with open('document.txt', 'r+') as f:
'x'
Create (error if present)
with open('document.txt', 'x') as f:
There are different files present on the computer. You are asked to add some lines to them.
The input contains 2 lines. The first line is the name of the file. The second one is the content we would like to add to that file.
The program needs to first add the specified line to the file and then print the whole content of the file.
Input
doc.txt (initial content)
Output
doc.txt (resulting content)
doc.txt This is the second line
This is the first line
This is the first line This is the second line
This is the first line This is the second line
Input
empty.txt (initial content)
Output
empty.txt (resulting content)
empty.txt Adding some content
Adding some content
Adding some content
Input
multiline.txt (initial content)
Output
multiline.txt (resulting content)
multiline.txt The final line!
This is some very very long document with many lines of text…
This is some very very long document with many lines of text… The final line!
This is some very very long document with many lines of text… The final line!
 

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue