Добавление в файл
Напомним, что есть несколько режимов работы с файлами:
Режим | Операция | Пример |
'r' | Чтение (по умолчанию) | with open('document.txt', 'r') as f: |
'w' | Запись | with open('document.txt', 'w') as f: |
'a' | Добавление | with open('document.txt', 'a') as f: |
'r+' | Чтение + Запись | with open('document.txt', 'r+') as f: |
'x' | Создание (ошибка при наличии) | with open('document.txt', 'x') as f: |
На компьютере есть различные файлы. Вам нужно добавить в них несколько строк.
Ввод содержит 2 строки. Первая строка — это имя файла. Вторая — содержимое, которое мы хотим добавить в этот файл.
Программа должна сначала добавить указанную строку в файл, а затем вывести всё содержимое файла.
Ввод | doc.txt (начальное содержимое) | Вывод | doc.txt (итоговое содержимое) |
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 |
Ввод | empty.txt (начальное содержимое) | Вывод | empty.txt (итоговое содержимое) |
empty.txt
Adding some content | ㅤ | Adding some content | Adding some content |
Ввод | multiline.txt (начальное содержимое) | Вывод | multiline.txt (итоговое содержимое) |
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: 1 seconds
Memory limit: 512 MB
Output limit: 1 MB