fb pixel

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
This is the second line

doc.txt (initial content)
This is the first line

Output
This is the first line
This is the second line

doc.txt (resulting content)
This is the first line
This is the second line
 
To check your solution you need to sign in
Sign in to continue