Aggiungere contenuto a un file

Ricordiamo le diverse modalità supportate per lavorare con i file:

Modalità

Operazione

Esempio

'r'

Lettura (quella predefinita)

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

'w'

Scrittura

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

'a'

Aggiunta

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

'r+'

Lettura + Scrittura

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

'x'

Creazione (errore se presente)

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

Ci sono diversi file presenti sul computer. Ti viene chiesto di aggiungere alcune righe a essi.

L'input contiene 2 righe. La prima riga è il nome del file. La seconda è il contenuto che desideriamo aggiungere a quel file.

Il programma deve prima aggiungere la riga specificata al file e poi stampare tutto il contenuto del file.

Input

doc.txt (contenuto iniziale)

Output

doc.txt (contenuto risultante)

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 (contenuto iniziale)

Output

empty.txt (contenuto risultante)

empty.txt
Adding some content

Adding some content

Adding some content

Input

multiline.txt (contenuto iniziale)

Output

multiline.txt (contenuto risultante)

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