An eine Datei anhängen

Erinnern Sie sich an die verschiedenen Modi, die zum Arbeiten mit Dateien unterstützt werden:

Modus

Operation

Beispiel

'r'

Lesen (Standard)

with open('document.txt', 'r') as f:

'w'

Schreiben

with open('document.txt', 'w') as f:

'a'

Anhängen

with open('document.txt', 'a') as f:

'r+'

Lesen + Schreiben

with open('document.txt', 'r+') as f:

'x'

Erstellen (Fehler, wenn vorhanden)

with open('document.txt', 'x') as f:

Auf dem Computer sind verschiedene Dateien vorhanden. Sie werden gebeten, einige Zeilen hinzuzufügen.

Die Eingabe enthält 2 Zeilen. Die erste Zeile ist der Name der Datei. Die zweite Zeile ist der Inhalt, den wir zu dieser Datei hinzufügen möchten.

Das Programm muss zuerst die angegebene Zeile zur Datei hinzufügen und dann den gesamten Inhalt der Datei ausgeben.

Eingabe

doc.txt (ursprünglicher Inhalt)

Ausgabe

doc.txt (ergänzter Inhalt)

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

Eingabe

empty.txt (ursprünglicher Inhalt)

Ausgabe

empty.txt (ergänzter Inhalt)

empty.txt
Adding some content

Adding some content

(leer)

Eingabe

multiline.txt (ursprünglicher Inhalt)

Ausgabe

multiline.txt (ergänzter Inhalt)

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